JavaScript为什么需要在同一行上调用匿名函数?

我读了一些有关闭包的文章,到处都看到了,但是没有明确的解释-每次我被告知要使用它时……:

// Create a new anonymous function, to use as a wrapper

(function(){

// The variable that would, normally, be global

var msg = "Thanks for visiting!";

// Binding a new function to a global object

window.onunload = function(){

// Which uses the 'hidden' variable

alert( msg );

};

// Close off the anonymous function and execute it

})();

好的,我看到我们将创建一个新的匿名函数,然后执行它。因此,在此之后,此简单的代码应该可以工作了(并且可以):

(function (msg){alert(msg)})('SO');

我的问题是这里发生了什么魔术?我以为在写的时候:

(function (msg){alert(msg)})

然后将创建一个新的未命名函数,如函数“”(msg)…

(function (msg){alert(msg)});

('SO');

为什么它需要在同一行?

您能给我一些帖子或给我一个解释吗?

回答:

将分号放在函数定义之后。

(function (msg){alert(msg)})

('SO');

以上应该工作。

如果您查看ECMA脚本规范,则可以通过3种方式定义函数。(第98页的第13节“功能定义”)

1.使用函数构造函数

var sum = new Function('a','b', 'return a + b;');

alert(sum(10, 20)); //alerts 30

2.使用函数声明。

function sum(a, b)

{

return a + b;

}

alert(sum(10, 10)); //Alerts 20;

3.函数表达式

var sum = function(a, b) { return a + b; }

alert(sum(5, 5)); // alerts 10

所以您可能会问,声明和表达式之间有什么区别?

根据ECMA脚本规范:

FunctionDeclaration:函数标识符(FormalParameterListopt){FunctionBody}

FunctionExpression:function

Identifieropt(FormalParameterListopt){FunctionBody}

如果您注意到,“ identifier” 对于函数表达式是 的。当您不提供标识符时,您将创建一个匿名函数。这并不意味着您无法指定标识符。

这意味着跟随是有效的。

var sum = function mySum(a, b) { return a + b; }

需要注意的重要一点是,只能在mySum函数主体内使用“ mySum”,而不能在外部使用。请参见以下示例:

var test1 = function test2() { alert(typeof test2); }

alert(typeof(test2)); //alerts 'undefined', surprise!

test1(); //alerts 'function' because test2 is a function.

比较一下

 function test1() { alert(typeof test1) };

alert(typeof test1); //alerts 'function'

test1(); //alerts 'function'


有了这些知识,让我们尝试分析您的代码。

当您有类似的代码时,

    function(msg) { alert(msg); }

您创建了一个函数表达式。您可以通过将其包装在括号内来执行此函数表达式。

    (function(msg) { alert(msg); })('SO'); //alerts SO.

以上是 JavaScript为什么需要在同一行上调用匿名函数? 的全部内容, 来源链接: utcz.com/qa/403250.html

回到顶部