(function(){})()之间的区别;和function(){}(); [英] Difference between (function(){})(); and function(){}();
问题描述
可能重复:
是“(function(){})()”和“(function(){}())”功能相同的JavaScript?
这是我还没想到的事情但是,我一直在使用function(){}()因为我的VIM语法突出显示我添加了括号,虽然我已经多次看到(function(){})(),也许它是一个IE东西?
This is something I haven't quite figured out yet, but I have been using function(){}() just because my VIM syntax highlight screws up if I add the parenthesis, although I've seen (function(){})() around many times, maybe its an IE thing?
编辑
var singleton = function() {
// code
}();
var singleton = (function() {
// code
})();
推荐答案
Peter Michaux讨论一对重要的Parens 。
Peter Michaux discusses the difference in An Important Pair of Parens.
基本上,括号是一个约定,表示紧接着调用的函数表达式,而不是普通函数。特别是如果函数体很长,这会减少意外,
Basically the parentheses are a convention to denote that an immediately invoked function expression is following, not a plain function. Especially if the function body is lengthy, this reduces surprises,
这篇关于(function(){})()之间的区别;和function(){}();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!