(function(){...}())之间有区别吗?和(function(){...})();? [英] Is there a difference between (function() {...}()); and (function() {...})();?

查看:114
本文介绍了(function(){...}())之间有区别吗?和(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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆