(function(){...}())之间有区别吗?和(function(){...})();? [英] Is there a difference between (function() {...}()); and (function() {...})();?
问题描述
可能重复:
自动执行匿名JavaScript函数的括号位置?
有时我看到:
(function() { ... }());
有时我看到:
(function() { ... })();
I see both forms with and without arguments. They both execute the anonymous function.
两种表格之间有区别吗?是否有任何令人信服的理由使用一种形式而不是另一种形式?
Is there a difference between the two forms? Are there any compelling reasons to use one form over the other?
推荐答案
这两种形式没有实际区别,但是一个语法观点,两者之间的区别在于分组运算符 - 括号 - 将在第一个示例中保存 CallExpression
,其中包括 FunctionExpression
:
There is no practical difference in those two forms, but from a grammatical point of view the difference between the two is that The Grouping Operator - the parentheses - will hold in the first example a CallExpression
, that includes the FunctionExpression
:
CallExpression
| |
FunctionExpression |
| |
V V
(function() { }());
^ ^
|--PrimaryExpression --|
在第二个例子中,我们首先得到一个完整的 CallExpression
,它持有 FunctionExpression
:
In the second example, we have first a whole CallExpression
, that holds the FunctionExpression
:
PrimaryExpression
|
FunctionExpression
|
V
(function() { })();
^ ^
|-- CallExpression --|
这篇关于(function(){...}())之间有区别吗?和(function(){...})();?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!