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

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

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