简体   繁体   English

(function(){})();和有什么区别? 和(function(){}());?

[英]What is the difference between (function(){})(); and (function(){}());?

Will the two following different lines of code do exactly the same? 以下两行不同的代码行会完全一样吗?

(function(){})();

(function(){}());

Yes. 是。 The only reason to include parentheses around the whole expression is to avoid its interpretation as a function declaration: 整个表达式中包含括号的唯一原因是避免将其解释为function声明:

(function f(){}());  // (1) Expression
(function f(){})();  // (2) Expression
function f(){}       // Function declaration
function f(){}();    // Syntax error

But whether you invoke the function literal directly (1) or have an intervening pair of parentheses (2) makes no difference at all. 但是,直接调用函数文字(1)还是插入一对括号(2)都没有任何区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM