2012-03-09 50 views
1

可能重複:
What is the !! (not not) operator in JavaScript?
Reference - What does this symbol mean in JavaScript?如何使用!!表達?

我找JS代碼寫這樣的:!未定義,!!假;

jquery的源代碼(jQuery的1.7.0.js:748線):

grep: function(elems, callback, inv) {  
    var ret = [], retVal;   
    inv = !!inv;   
    // Go through the array, only saving the items  
    // that pass the validator function   
    for (var i = 0, length = elems.length; i < length; i++){    
     retVal = !!callback(elems[ i ], i);    
     if (inv !== retVal) {     
      ret.push(elems[ i ]);    
     }   
    }   
    return ret;  
} 
+0

那麼,'!expr'呢? '!(!expr)'做什麼?把小部分放在一個更大的部分:) – 2012-03-09 05:28:00

+1

可能重複[參考 - 這個符號在JavaScript中意味着什麼?](http://stackoverflow.com/questions/9549780/reference-what-does-this-symbol-意思是在JavaScript中),更具體的[什麼是! (不是)運營商在JavaScript?](http://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript) – 2012-03-09 05:30:14

回答

1

!opposite裝置

所以!!裝置double opposite

它也常用在這種情況下使用:

var check = !!(window.something && window.runthis) 
//If something exists and runthis exists, then 
//check = true 

//If one of them is not exist, then 
//check = false 

checking browser compatibly常用。

0

這是鑄造一個非布爾對象到布爾值的簡略方式。

例如,!! 0會轉換爲false。