1

Did anyone know the code which define like this:

!function(window, undefined) {
    // do something
} (window)

By searching in google, I can understand the syntax like:

function(window, undefined) {
    // do something
} (window)

But I don't figure out any article about the syntax have "!" operator.

4

2 回答 2

3

运算符在!那里,因此函数被解析为表达式,而不是声明。由于无法调用声明,因此您的第二个示例是语法错误。

更常见的形式是将函数括在括号中:

(function(window,undefined) {
    // do something
}(window));

这与任何一元运算符的使用具有完全相同的效果。

于 2013-06-03T06:57:52.720 回答
0

可能是这样的。

!(function(window, undefined){ /* some code */ })(window);
(function(window, undefined){ /* some code */ })(window);
于 2013-06-03T06:58:02.107 回答