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

// 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 () {
    return ( 10 + 20 );
})();

Peter Michaux在《一对重要的圆括号》中讨论了两者的区别。

当试图从父名称空间隐藏变量时,它是一个有用的构造。函数中的所有代码都包含在函数的私有作用域中,这意味着它根本不能从函数外部访问,这使得它真正是私有的。

See:

闭合(计算机科学) JavaScript命名空间 Javascript中重要的一对括号

其他回答

这个答案与问题没有严格的联系,但是您可能有兴趣发现这种语法特性并不是函数特有的。例如,我们总是可以这样做:

alert(
    {foo: "I am foo", bar: "I am bar"}.foo
); // alerts "I am foo"

与函数相关。因为它们是继承自Function的对象。原型,我们可以这样做:

Function.prototype.foo = function () {
    return function () {
        alert("foo");
    };
};

var bar = (function () {}).foo();

bar(); // alerts foo

你知道,我们甚至不需要用圆括号来包围函数来执行它们。不管怎样,只要我们试着把结果赋值给一个变量。

var x = function () {} (); // this function is executed but does nothing

function () {} (); // syntax error

对于函数,一旦声明了它们,就可以对它们调用new操作符并获得一个对象。以下是等价的:

var obj = new function () {
    this.foo = "bar";
};

var obj = {
    foo : "bar"
};

它被称为自调用函数。

当你调用(function(){})时,你所做的是返回一个函数对象。当您将()追加到它时,它将被调用,并执行主体中的任何内容。的;表示语句的结束,这就是第二次调用失败的原因。

不带括号的例子:

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也是其中之一)。不工作的当然是++,——因为有一个变量的要求。

换行符是不必要的。

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

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".

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

匿名函数使用函数操作符声明 函数声明的。可以使用函数操作符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/