English 中文(简体)
JS Functions & IIFE
原标题:JS Functions & IIFE

当我操作以下联合材料时:

var x = (function () {
    console.log( Hello x! );
});

var y = function () {
    console.log( Hello y! );
};

var z = (function () {
    console.log( Hello z! )
    return 2;
})();

x();

y();

以下产出在册子上印成:

Hello z!

Hello x!

Hello y!

为什么“Hello z!”在“前印发。 Hello xamp; “ Hello y!。 立即发表的功能表达方式如何与变数相互作用?

P.S.:我是开端人。

问题回答

由于这是欧洲常规武装力量:

var z = (function () {
    console.log( Hello z! )
    return 2;
})();

如果没有《欧洲常规武装力量条约》高于法律,可以改写如下:

var x = (function () {
    console.log( Hello x! );
});

var y = function () {
    console.log( Hello y! );
};

var z = function () {
    console.log( Hello z! )
    return 2;
};
z();

x();

y();

Hello z为什么首先印刷

如同上述答复一样,这套电话指《欧洲常规武装力量章程》第一版,然后是X功能,然后是功能。 如果你想要有很好的视觉代表,则由Philip Roberts(进行检查。 它让你通过它如何呼吁和执行来撰写法典和步骤。 在试图理解执行顺序时帮助我很多。

The first I in IIFE means Immediately.
That s why this function is executed first.

In addition, the IIFE can not interact with the z variable.
But you can pass arguments to it like this :

var hi =  Hello z! ;

var z = (function (txt) {
    console.log(txt)
})(hi);

希望这将有助于。

The first two vars, x and y y are assigned function Value,而第三者则被立即印成功能表述所退回的价值:

(function () { console.log( Hello z! ); return 2; }) // <= this is an expression

附加说明的<条码>(......)使功能声明成为一种功能,而这一功能立即通过添加<条码>(<>>>>。 援引的职能回报<条码>2分配给<条码>z,而术语输出是执行功能的副作用。

其后,您的代码继续援引<代码>x();y(,其中将其体码登录到代号<><>>>>><>>>>>>>> ;> 和y(<>>,在开始z<>/code>时,援引。

IIFEcanz相互作用,但z在本职能中始终为未定义的,尽管其执行效果并不大,因为虽然它已经宣布,但尚未分配价值。 该表是在国际不动产业协会完成后分配的,该功能的回报值为2





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.