當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


underscore.js _.negate()用法及代碼示例

Underscore.js是一個JavaScript庫,它提供了許多有用的函數,即使不使用任何內置對象,它們也可以以很大的方式幫助編程,例如映射,過濾器,調用等。

_.negate()函數是JavaScript的Underscore.js庫中的內置函數,用於查找聲明的謂詞函數的新否定版本。

用法:

_.negate(predicate)

參數:它接受以下指定的單個參數:

  • predicate:它是陳述的謂詞函數。

返回值:此方法返回所聲明謂詞函數的新否定版本。



範例1:

<!DOCTYPE html> 
<html> 
  
<head> 
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> 
    </script> 
</head> 
  
<body> 
    <script> 
        var isNaN = _.negate(Boolean); 
          
        console.log(_.find( 
            [3, -11, undefined, 41, 0], isNaN)); 
    </script> 
</body> 
  
</html>

輸出:

undefined

範例2:

<!DOCTYPE html> 
<html> 
  
<head> 
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> 
    </script> 
</head> 
  
<body> 
    <script> 
        function isCSportal(character) { 
            return character === "GeeksforGeeks"; 
        } 
  
        var isNotCSportal = _.negate(isCSportal); 
        console.log(isNotCSportal("GeeksforGeeks")); 
        console.log(isNotCSportal("GfG")); 
    </script> 
</body> 
  
</html>

輸出:

false
 true

參考:https://underscorejs.org/#negate




相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Underscore.js _.negate() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。