2012-03-09 49 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。