为什么使用括号来包装 javascript 函数调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6645766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are parenthesis used to wrap a javascript function call?
提问by Dexy_Wolf
What is the difference between these two javascript function calls?
这两个javascript函数调用有什么区别?
(function(){alert("foo")})()
versus this:
与此相反:
(function(){alert("foo")}())
采纳答案by keparo
This is done for readability.
这样做是为了可读性。
There isn't a real functional difference between the two examples you've given, but both of them are very close to a simple function declaration, which isdifferent. The parenthesis are added for readability, in order to distinguish them.
您给出的两个示例之间没有真正的功能差异,但是它们都非常接近于简单的函数声明,这是不同的。添加括号是为了可读性,以便区分它们。
Here is what each of your snippets do:
以下是您的每个片段的作用:
In the first of your two snippets, the first parenthesis will be evaluated as the value of the enclosed function. Then this value will be called as a function. So ultimately the function will be executed, which is probably what you care about.
在您的两个片段中的第一个中,第一个括号将被评估为封闭函数的值。然后这个值将作为一个函数被调用。所以最终会执行该函数,这可能是您关心的。
In your second snippet, the outer parenthesis will be evaluated as containing a function which is declared inline and immediately executed. Again, the function will be executed, which is still probably what you care about.
在您的第二个代码段中,外括号将被评估为包含一个声明为内联并立即执行的函数。同样,该函数将被执行,这可能仍然是您关心的。
Both of these will execute the same function, so there won't be any significant difference.
这两者都将执行相同的功能,因此不会有任何显着差异。
The difference between a snippet like yours and a simple function declaration:
像你这样的代码片段和简单的函数声明之间的区别:
The functions you've given are also identical to the following. I've just added a function name and assigned the return value for syntactical accuracy, which you can ignore for now.
您提供的功能也与以下相同。我刚刚添加了一个函数名并为语法准确性分配了返回值,您现在可以忽略它。
// javascript...
var val =
function myFooFunc () {
alert("foo");
}();
However, this would be easily mistaken for a simple function declaration, which is different:
然而,这很容易被误认为是一个简单的函数声明,它是不同的:
// javascript...
function myFooFunc () {
alert("foo");
}
Notice that the only real difference here is that this last function declaration is not executed immediately. The others are. So that is a very different behavior (the simple declaration may be executed later if it is called by name, or it may not ever execute at all). It's often hard to see that difference in the syntax right away, however, especially if the function body grows to be very long and requires scrolling on the screen.
请注意,这里唯一真正的区别是最后一个函数声明不会立即执行。其他都是。所以这是一个非常不同的行为(如果按名称调用简单声明可能会在稍后执行,或者它可能永远不会执行)。然而,通常很难立即看出语法上的差异,尤其是当函数体变得很长并且需要在屏幕上滚动时。
Why are functions executed immediately?
为什么函数会立即执行?
When a function is immediately executed after it is declared, the value is often being returned to something (it may be part of an assignment statement). Sometimes the function is being executed right away because it contains inner functions and is being used to provide functional scope to the inclosed statements.
当一个函数在声明后立即执行时,它的值通常被返回给某个东西(它可能是赋值语句的一部分)。有时该函数会立即执行,因为它包含内部函数并用于为封闭语句提供功能范围。
Essentially, people wrap parenthesis around the "executed immediately" form (both of your snippets, and the first one of my two) in order to give a visual cue to other developers that the function is being called immediately. It's just easier to read, since you might not catch the parenthesis until you got to the end of the function (or notice them at all).
从本质上讲,人们将括号括在“立即执行”形式(您的两个片段和我的两个片段中的第一个)周围,以便向其他开发人员提供一个视觉提示,即该函数正在被立即调用。它更容易阅读,因为在到达函数末尾之前(或根本没有注意到它们),您可能无法抓住括号。
回答by csano
They both have similar behaviors.
他们都有相似的行为。
The parentheses encapsulating the function declaration tell the JavaScript engine to execute the code immediately after it's parsed. In the first example, you're creating a function object then invoking it with the parentheses that follow. In the second example, you are telling the JavaScript engine to create the function object and invoke it immediately.
封装函数声明的括号告诉 JavaScript 引擎在解析后立即执行代码。在第一个示例中,您正在创建一个函数对象,然后使用后面的括号调用它。在第二个示例中,您告诉 JavaScript 引擎创建函数对象并立即调用它。
Example:
例子:
// creates a function object
var f1 = (function() { alert('foo'); });
// creates a function object and executes it immediately
var f2 = (function() { alert('foo'); }());
The difference is that f1 gives you a function object. f2 creates and invokes an anonymous function.
不同之处在于 f1 为您提供了一个函数对象。f2 创建并调用匿名函数。