(……)和JavaScript闭包中的(…)

Camilo Martin提出了一个问题:(…()) vs. (…)() in javascript closures [duplicate],或许与您遇到的问题类似。

回答者Adam Rackis给出了该问题的处理方式:

There's no difference. Both are valid ways to get the JavaScript parser to treat your function as an expression instead of a declaration.

Note that + and ! will also work, and are sometimes used by minifiers to save a character of size:

+function() {  
    var foo = 'bar';  
}();

!function() {  
    var foo = 'bar';  
}();

EDIT

As @copy points out, for completeness, ~ and - will also work.

-function() {  
    var foo = 'bar';  
}();

~function() {  
    var foo = 'bar';  
}();

希望本文对你有帮助,欢迎支持JavaScript中文网

原文链接:stackoverflow.com

上一篇:在CSS中使用JavaScript
下一篇:可以clearinterval()被称为内setinterval()?

相关推荐

官方社区

扫码加入 JavaScript 社区