Node.js util.types.isUint8Array() 方法
util 模块的util.types.isUint8Array() 方法,主要用于支持 Node.js 自己的内部 API 的需求。用于检查方法中传入的实例是否为内置的 Uint8Array 实例。
句法
util.types.isUint8Array( value )
参数:此方法接受一个包含任何值的单个参数值,即任何模块的实例。
返回值:此方法返回一个布尔值,即如果传递的值是 Uint8Array 的实例,则返回 true,否则返回false 。
下面的示例说明了 Node.js 中util.types.isUint8Array() 方法的使用:
示例 1:
// Node.js program to demonstrate the
// util.types.isUint8Array() method
// It includes util module
const util = require('util');
// Return false as passed instance is of set
console.log(util.types.isUint8Array(new Set()));
// Return true as passed instance is of Uint8Array
console.log(util.types.isUint8Array(new Uint8Array()));
输出:
false
true
示例 2:
// Node.js program to demonstrate the
// util.types.isUint8Array() method
// It includes util module
const util = require('util');
// Making an instance of Uint8Array
// of size 2
var array1 = new Uint8Array(2);
// Initializing the zeroth element
array1[0] = 42;
// Returns true as passed instance
// is of Uint8Array
console.log(util.types.isUint8Array(array1));
// Making an instance of Uint8Array
var array2 = new Uint8Array([21, 31]);
// Returns true as passed instance
// is of Uint8Array
console.log(util.types.isUint8Array(array2));
// Making an instance of Uint8Array
var array3 = new Uint8Array([21, 31]);
// Making another instance of Uint8Array by
// passing an instance of Uint8Array
var array4 = new Uint8Array(array3);
// Returns true as passed instance
// is of Uint8Array
console.log(util.types.isUint8Array(array4));
// Making an instance of Int16Array of size 2
var array5 = new Int16Array(2);
// Initializing the zeroth element
array5[0] = 10;
// Returns false as passed instance
// is of Int16Array
console.log(util.types.isUint8Array(array5));
输出:
true
true
true
false
参考: https://nodejs.org/api/util.html#util_util_types_isuint8array_value