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

发布于 2024-09-24 09:22:53 字数 655 浏览 2 评论 0 原文

可能的重复:
自动执行匿名 JavaScript 函数的括号位置?

有时我看到:

(function() { ... }()); 

有时我看到:

(function() { ... })(); 

我看到带参数和不带参数的两种形式。他们两者 两者 net/WZ3X2/" rel="nofollow noreferrer">执行匿名函数。

两种形式有区别吗?是否有任何令人信服的理由来使用一种形式而不是另一种形式?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉生梦死 2024-10-01 09:22:53

这两种形式没有实际区别,但从语法的角度来看,两者之间的区别在于分组运算符 - 括号 - 将在第一个示例中保存 CallExpression,其中包括 FunctionExpression

               CallExpression
                |         |
       FunctionExpression |
                |         |
                V         V
    (function() {       }());
    ^                      ^
    |--PrimaryExpression --|

在第二个示例中,我们首先有一个完整的 CallExpression,其中包含 FunctionExpression

          PrimaryExpression
                |
         FunctionExpression
                |
                V
    (function() {       })();
    ^                      ^
    |--  CallExpression  --|

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  --|

爱的故事 2024-10-01 09:22:53

就编译器而言,两者之间没有区别。不过,会发现 Douglas Crockford 的 JavaScript (function () {}()) 样式“nofollow">代码约定

There is no difference between the two, so far as the compiler is concerned. However, will find that the (function () {}()) style is recommended in Douglas Crockford’s JavaScript code conventions.

笔芯 2024-10-01 09:22:53

就差异而言,它实际上只是语法糖。有点相当于:“你喜欢 jQuery() 还是 $()?”两者都可以编译、执行和互换使用(AFAIK)。

从迄今为止我看到的代码示例来看,似乎有更多的人遵循 Crockford 代码约定:

(function() { ... }()); 

就我个人而言,我更喜欢 (function(){})(); 约定,因为它对我来说更明显该函数是自动执行的;我也是 jQuery 的大用户,这是 jQuery 源代码中使用的约定。

此外,无论您选择使用哪种形式,使用括号括起自执行函数都被认为是一种很好的做法。

As far as differences go, it is really just syntactic sugar. Somewhat equivalent to: "do you like jQuery() or $()?" Both can be compiled, executed, and used interchangeably (AFAIK).

From the code samples I have seen thus far, more people seem to follow the Crockford code convention:

(function() { ... }()); 

Personally, I prefer the (function(){})(); convention because it is more apparent to me that the function is self-executing; I'm also a big user of jQuery and that's the convention used in jQuery source.

Additionally, it is considered good practice to use parens to enclose your self-executing function, regardless of which form you choose to go with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文