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


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

_.not()方法r返回一個布爾值,該布爾值與給定值的真實性布爾值相反。

用法:

_.not( value );

參數:

  • value:給定值,用於返回相反的布爾值。

返回值:此方法返回一個布爾值。

注意:由於它需要安裝underscore.js contrib庫,因此在常規JavaScript中將無法使用。可以使用npm install underscore-contrib -save安裝Underscore.js contrib庫。



範例1:對於此方法,它返回false。

Javascript

// Defining underscore contrib variable 
var _ = require('underscore-contrib');  
  
var bool = _.not( true ); 
  
console.log("Opposite boolean value if"
    + " value's truthiness is:", bool);

輸出:

Opposite boolean value if value's truthiness is: false

範例2:如果為false,則此方法返回與true相反的值。

Javascript

// Defining underscore contrib variable 
var _ = require('underscore-contrib');  
  
var bool = _.not( false ); 
  
console.log("Opposite boolean value if"
    + " value's truthiness is:", bool);

輸出:

Opposite boolean value if value's truthiness is: true

範例3:對於現有值,此方法返回false。

Javascript

// Defining underscore contrib variable 
var _ = require('underscore-contrib');  
  
var bool = _.not( "Geeks" ); 
  
console.log("Opposite boolean value if"
    + " value's truthiness is:",bool);

輸出:

Opposite boolean value if value's truthiness is: false

範例4:對於不存在的值,此方法返回true。

Javascript

// Defining underscore contrib variable 
var _ = require('underscore-contrib');  
  
var bool = _.not( null ); 
  
console.log("Opposite boolean value if"
    + " value's truthiness is:", bool);

輸出:

Opposite boolean value if value's truthiness is: true

相關用法


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