util模塊的util.types.isNumberObject()方法主要旨在滿足Node.js自己的內部API的需求。用於檢查傳遞的值是否為Number對象。
用法
util.types.isNumberObject( value )
參數:此方法接受單個參數值,該值可保存任何數字,即任何模塊的實例。
返回值:此方法返回布爾值,即,如果傳遞的值是Number對象,則為true,否則返回false。
以下示例說明了Node.js中util.types.isNumberObject()方法的使用:
範例1:
// Node.js program to demonstrate the
// util.types.isNumberObject() method
// It includes util module
const util = require('util');
// Returns false as it is not a number object
console.log(util.types.isNumberObject(0));
// Returns true as it is a number object
console.log(util.types.isNumberObject(new Number(0)));
輸出:
false true
範例2:
// Node.js program to demonstrate the
// util.types.isNumberObject() method
// It includes util module
const util = require('util');
// Returns false becaluse it is not a number object
console.log(util.types.isNumberObject(100));
// Returns true because it is a number object
console.log(util.types.isNumberObject(new Number(12345678)));
// returns false because it is not a number object
console.log(util.types.isNumberObject(1.8));
// Returns true because it is a number object
console.log(util.types.isNumberObject(new Number(1.8)));
// Returns false as Number() method just convert
// string to number not to Number Object
console.log(util.types.isNumberObject(Number('1.8')));
輸出:
false true false true false
參考: https://nodejs.org/api/util.html#util_util_types_isnumberobject_value
相關用法
注:本文由純淨天空篩選整理自akshajjuneja9大神的英文原創作品 Node.js | util.types.isNumberObject() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。