2015-01-31 56 views

回答

2

完全一样。

// This one creates a function expression, then executes that function expression. 
var foo = function(){ 
    return { }; 
}(); 

// This one creates a function expression, inside of a set of parens. 
// the parens hold an expression. 
var foo = (function(){ 
    return { }; 
}()); 

的括号用于两个原因:

1)在这种情况下,他们是一个线索的读者,而不是编译器,你有一个IIFE。

2)在其他上下文中,当可能生成函数语句时,parens强制执行表达式。

// The parens here force an expression, which means it forces a function expression 
// instead of a function statement. 
(function() {....}) 
+0

谢谢...现在我的想法是明确的:) – 2015-01-31 19:31:42