繁体   English   中英

语法是什么!function(){...}是什么意思?

[英]What does the syntax !function() { … } mean?

我在简单,优秀,精彩和强大的库knockoutjs中找到了这种语法:

!function(factory) { ... }

function声明之前,not符号( ! )的含义是什么?

更新:源代码不再包含此确切语法。

! 运算符表现正常,否定表达式。 在这种情况下,它用于强制函数为函数表达式而不是函数语句。 自从! 运算符必须应用于表达式(将它应用于语句没有意义,因为语句没有值),该函数将被解释为表达式。

这样就可以立即执行。

function(){
    alert("foo");
}(); // error since this function is a statement, 
     // it doesn't return a function to execute

!function(){
    alert("foo");
}(); // This works, because we are executing the result of the expression
// We then negate the result. It is equivalent to:

!(function(){
    alert("foo");
}());

// A more popular way to achieve the same result is:
(function(){
    alert("foo");
})();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM