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


Node.js util.types.isBoxedPrimitive()用法及代碼示例


util.types.isBoxedPrimitive()方法是util模塊的內置應用程序編程接口,其主要設計用於滿足Node.js自己的內部API的需求。

util.types.isBoxedPrimitive()方法用於檢查給定值是否為盒裝原語。

用法:


util.types.isBoxedPrimitive( value )

參數:該函數接受上麵提到並在下麵描述的一個參數:

  • value:它是將對裝箱原語進行檢查的值。

返回值:它返回一個布爾值,即如果傳遞的值是一個裝箱的原語,則返回true,否則返回false。

以下示例說明了Node.js中的util.types.isBoxedPrimitive()方法:

範例1:

// Node.js program to demonstrate the     
// util.types.isBoxedPrimitive() method 
  
// Import the util module 
const util = require('util'); 
  
// Checking an unboxed string 
let unboxedString = "GeeksforGeeks"; 
console.log("Value:", unboxedString); 
isBoxed = util.types.isBoxedPrimitive(unboxedString); 
console.log("Value is a boxed primitive:", isBoxed); 
  
// Checking a boxed string 
let boxedString = new String("GeeksforGeeks"); 
console.log("Value:", boxedString); 
isBoxed = util.types.isBoxedPrimitive(boxedString); 
console.log("Value is a boxed primitive:", isBoxed); 
  
// Checking an unboxed integer 
let unboxedInteger = 99; 
console.log("Value:", unboxedInteger); 
isBoxed = util.types.isBoxedPrimitive(unboxedInteger); 
console.log("Value is a boxed primitive:", isBoxed); 
  
// Checking a boxed integer 
let boxedInt = new Number(99); 
console.log("Value:", boxedInt); 
isBoxed = util.types.isBoxedPrimitive(boxedInt); 
console.log("Value is a boxed primitive:", isBoxed); 
  
// Checking an unboxed boolean 
let unboxedBool = false; 
console.log("Value:", unboxedBool); 
isBoxed = util.types.isBoxedPrimitive(unboxedBool); 
console.log("Value is a boxed primitive:", isBoxed); 
  
// Checking a boxed boolean 
let boxedBool = new Boolean(false); 
console.log("Value:", boxedBool); 
isBoxed = util.types.isBoxedPrimitive(boxedBool); 
console.log("Value is a boxed primitive:", isBoxed);

輸出:

Value:GeeksforGeeks
Value is a boxed primitive:false
Value:[String:'GeeksforGeeks']
Value is a boxed primitive:true
Value:99
Value is a boxed primitive:false
Value:[Number:99]
Value is a boxed primitive:true
Value:false
Value is a boxed primitive:false
Value:[Boolean:false]
Value is a boxed primitive:true

範例2:

// Node.js program to demonstrate the     
// util.types.isBoxedPrimitive() method 
  
// Import the util module 
const util = require('util'); 
  
// Checking for the symbol primitive 
let symbol = Symbol('geeks'); 
console.log("Value:", symbol); 
isBoxed = util.types.isBoxedPrimitive(symbol); 
console.log("Value is a boxed primitive:", isBoxed); 
  
// Checking again after creating a symbol wrapper 
let symbolObj = new Object(Symbol('geeks')); 
console.log("Value:", symbolObj); 
isBoxed = util.types.isBoxedPrimitive(symbolObj); 
console.log("Value is a boxed primitive:", isBoxed);
Value:Symbol(geeks)
Value is a boxed primitive:false
Value:[Symbol:Symbol(geeks)]
Value is a boxed primitive:true

參考: https://nodejs.org/api/util.html#util_util_types_isboxedprimitive_value



相關用法


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