之间有任何区别
var myfunc = (function () { return function () { ... } }());
Run Code Online (Sandbox Code Playgroud)
和
var myfunc = function () { return function () { ... } }();
Run Code Online (Sandbox Code Playgroud)
它只是一种风格问题还是第一种形式的周围()更多?
不.或者至少不在你的例子中.
只有当function关键字是语句中的第一个标记时,外部parens才有意义.
// cool
var foo = function(){}();
var foo = (function(){}());
// also cool
(function(){}());
// not cool, syntax error
// parsed as function statement, expects function name which is missing
function(){}();
// also not cool, syntax error
// declares a function, but can't be executed immediately
function foo(){}();
Run Code Online (Sandbox Code Playgroud)
何时function是语句中的第一个标记,它是一个函数声明(思考命名函数),其行为与function所有其他上下文中的行为略有不同.领先的paren强制解析器将其视为函数表达式(想想匿名函数),这样就可以立即执行.
请参阅:JavaScript中的函数表达式与声明之间有什么区别?
如果你用其他东西开始行或语句,比如变量声明,它在技术上根本不重要.
| 归档时间: |
|
| 查看次数: |
123 次 |
| 最近记录: |