我读了一些关于闭包的帖子,到处都看到了这个,但是没有明确的解释它是如何工作的——每次我都被告知要使用它…:

// 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)})

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

但为什么这行不通呢?

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

为什么要在同一条线上?

你能给我指出一些帖子或者给我一个解释吗?


当前回答

它不起作用的简单原因不是因为;表示匿名函数结束。这是因为在函数调用的末尾没有(),它就不是函数调用。也就是说,

function help() {return true;}

如果调用result = help();这是对函数的调用,返回true。

如果你拨打result = help;这不是电话。在这种分配中,帮助被视为要分配给结果的数据。

你所做的是通过添加分号来声明/实例化一个匿名函数,

(function (msg) { /* Code here */ });

然后尝试在另一个语句中调用它,只使用括号…显然,因为函数没有名称,但这将不起作用:

('SO');

解释器将第二行上的括号视为一个新的指令/语句,因此它不起作用,即使你这样做:

(function (msg){/*code here*/});('SO');

它仍然不能工作,但当您删除分号时,它可以工作,因为解释器会忽略空白和空格,并将完整的代码视为一条语句。

(function (msg){/*code here*/})        // This space is ignored by the interpreter
('SO');

Conclusion: a function call is not a function call without the () on the end unless under specific conditions such as being invoked by another function, that is, onload='help' would execute the help function even though the parentheses were not included. I believe setTimeout and setInterval also allow this type of function call too, and I also believe that the interpreter adds the parentheses behind the scenes anyhow which brings us back to "a function call is not a function call without the parentheses".

其他回答

这就是JavaScript的工作原理。你可以声明一个命名函数:

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

叫它:

foo("Hi!");

或者,你可以声明一个匿名函数:

var foo = function (msg) {
    alert(msg);
}

称之为:

foo("Hi!");

或者,你可以永远不将函数绑定到一个名称:

(function(msg){
   alert(msg);
 })("Hi!");

函数也可以返回函数:

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

(make_foo())("Hi!");

make_foo函数体中任何用“var”定义的变量都将被make_foo返回的每个函数关闭,这是毫无意义的。这是一个闭包,这意味着一个函数对值所做的任何更改都将被另一个函数可见。

这让你可以封装信息,如果你想:

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

var hello = make_greeter("Hello!");

hello();

除了Java,几乎所有编程语言都是这样工作的。

当你这样做的时候:

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

由于分号的原因,您在('SO')之前结束了函数。如果你这样写:

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

它会起作用的。

工作示例:http://jsfiddle.net/oliverni/dbVjg/

我感到困惑的一件事是“()”是分组操作符。

这是你声明的基本函数。

例1:

var message = 'SO';

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

foo(message);

函数是对象,可以分组。我们在函数周围加上括号。

例2:

var message = 'SO';

function foo(msg) {  //declares foo
    alert(msg);
}

(foo)(message);     // calls foo

现在,我们不用声明并立即调用相同的函数,而是可以在调用时使用基本替换来声明它。

例3。

var message = 'SO';

(function foo(msg) {
    alert(msg);
})(message);          // declares & calls foo

最后,我们不需要额外的foo,因为我们没有使用名称来调用它!函数可以是匿名的。

例4。

var message = 'SO';

(function (msg) {   // remove unnecessary reference to foo
    alert(msg);
})(message);

要回答您的问题,请参考示例2。第一行声明了一些无名函数并对其进行分组,但不调用它。第二行对字符串进行分组。两者都不做任何事。(文森特的第一个例子。)

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

(foo); 
(msg); //Still nothing.

But

(foo)
(msg); //works

不带括号的例子:

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

(这是void的唯一真正用途,afaik)

or

var a = function (msg) { alert(msg); }
('SO');

or

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

同时也要工作。void导致表达式求值,以及赋值和bang。最后一个可以使用~、+、-、delete、typeof和一些一元操作符(void也是其中之一)。不工作的当然是++,——因为有一个变量的要求。

换行符是不必要的。

匿名函数是动态声明的函数 运行时。它们被称为匿名函数,因为它们不是 以与普通函数相同的方式命名。

匿名函数使用函数操作符声明 函数声明的。可以使用函数操作符to 在任何可以放置表达式的地方创建一个新函数。为 例如,您可以将一个新函数声明为参数 函数调用或分配另一个对象的属性。

下面是一个命名函数的典型例子:

function flyToTheMoon() {
    alert("Zoom! Zoom! Zoom!"); 
}

flyToTheMoon(); 

下面是创建匿名函数的相同示例:

var flyToTheMoon = function() {
   alert("Zoom! Zoom! Zoom!"); 
}

flyToTheMoon();

详情请浏览http://helephant.com/2008/08/23/javascript-anonymous-functions/