(function() {...}()); 之间有区别吗?和 (function() {...})();? [英] Is there a difference between (function() {...}()); and (function() {...})();?
问题描述
可能的重复:
自动执行匿名 JavaScript 函数的括号位置?
有时我看到:
(function() { ... }());
有时我会看到:
(function() { ... })();
这两种形式有区别吗?使用一种形式而不使用另一种形式有什么令人信服的理由吗?
这两种形式没有实际区别,但从语法的角度来看,两者的区别在于CallExpression
,包括 FunctionExpression
:
在第二个例子中,我们首先有一个完整的 CallExpression
,它保存了 FunctionExpression
:
Possible Duplicate:
Location of parenthesis for auto-executing anonymous JavaScript functions?
Sometimes I see:
(function() { ... }());
and sometimes I see:
(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?
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 --|
In the second example, we have first a whole CallExpression
, that holds the FunctionExpression
:
PrimaryExpression | FunctionExpression | V (function() { })(); ^ ^ |-- CallExpression --|
这篇关于(function() {...}()); 之间有区别吗?和 (function() {...})();?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!