📜  Underscore.js _.truthy() 方法

📅  最后修改于: 2022-05-13 01:56:53.453000             🧑  作者: Mango

Underscore.js _.truthy() 方法

这 _。 truthy () 方法检查给定值是否为真,相应地返回一个布尔值。真值是在布尔上下文中强制为真的

句法:

_.truthy( value );

参数:

  • value:要检查真实值的给定值。

返回值:此方法返回一个布尔值(如果给定值为真则返回真,否则返回假)。

注意:这在普通 JavaScript 中不起作用,因为它需要安装 underscore.js contrib 库。

Underscore.js contrib 库可以使用npm install underscore-contrib –save 安装。

示例 1: True 布尔值始终返回 true。

Javascript
// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var bool = _.truthy(true);
  
console.log("Given Value is Truthy : ",bool);


Javascript
// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var bool = _.truthy(10);
  
console.log("Given Value is Truthy : ",bool);


Javascript
// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var bool = _.truthy([ 1, 2, 3 ]);
  
console.log("Given Value is Truthy : ",bool);


Javascript
// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var bool = _.truthy(false);
  
console.log("Given Value is Truthy : ",bool);


输出:

Given Value is Truthy : true

示例 2:现有值返回 true 布尔值,因此此方法返回 true。

Javascript

// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var bool = _.truthy(10);
  
console.log("Given Value is Truthy : ",bool);

输出:

Given Value is Truthy : true

示例 3:

Javascript

// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var bool = _.truthy([ 1, 2, 3 ]);
  
console.log("Given Value is Truthy : ",bool);

输出:

Given Value is Truthy : true

示例 4:在传递 false 布尔值时,此方法返回 false。

Javascript

// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var bool = _.truthy(false);
  
console.log("Given Value is Truthy : ",bool);

输出:

Given Value is Truthy : false