且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

(function(){...}())之间有区别吗?和(function(){...})();?

更新时间:2023-12-04 11:35:58

这两种形式没有实际区别,但是一个语法观点,两者之间的区别在于分组运算符 - 括号 - 将在第一个示例中保存 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  --|