簡體   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