util模块的util.types.isDataView()方法方法主要旨在满足Node.js自己的内部API的需求。 util.types.isDataView()方法用于检查给定值是否为DataView对象。
用法:
util.types.isDataView(value)
参数:该函数接受上面提到并在下面描述的一个参数:
- value:它是要为DataView对象检查的值。
返回值:此方法返回一个布尔值,即如果传递的值是DataView的实例,则为true,否则返回false。
以下程序说明了Node.js中的util.types.isDataView()方法:
范例1:
Node.js
// Node.js program to demonstrate the
// util.types.isDataView() method
// Import the util module
const util = require('util');
// Checking for a DataView
let arraybuf = new ArrayBuffer(5);
let dataView = new DataView(arraybuf);
console.log("Value:", dataView);
isDataView = util.types.isDataView(dataView);
console.log("Value is a DataView:", isDataView);
// Checking for an Integer array
let intArray = new Int32Array();
console.log("Value:", intArray);
isDataView = util.types.isDataView(intArray);
console.log("Value is a DataView:", isDataView);
输出:
Value:DataView { byteLength:5, byteOffset:0, buffer:ArrayBuffer { [Uint8Contents]: <00 00 00 00 00>, byteLength:5 } } Value is a DataView:true Value:Int32Array [] Value is a DataView:false
范例2:
Node.js
// Node.js program to demonstrate the
// util.types.isDataView() method
// Import the util module
const util = require('util');
// Creating an unsigned integer 8-bit array
let int8Array = new Uint8Array([10, 20, 30]);
console.log("Value:", int8Array);
// Checking for a view
console.log("Value is a View:",
ArrayBuffer.isView(int8Array));
// Checking for a DataView
isDataView = util.types.isDataView(int8Array);
console.log("Value is a DataView:", isDataView);
// Creating a dataview
let dataView = new DataView(new ArrayBuffer(3));
dataView.setUint8(0, 10);
dataView.setUint8(1, 20);
dataView.setUint8(2, 30);
console.log("Value:", dataView);
// Checking for a view
console.log("Value is a View:",
ArrayBuffer.isView(dataView));
// Checking for a DataView
isDataView = util.types.isDataView(dataView);
console.log("Value is a DataView:", isDataView);
输出:
Value:Uint8Array [ 10, 20, 30 ] Value is a View:true Value is a DataView:false Value:DataView { byteLength:3, byteOffset:0, buffer:ArrayBuffer { [Uint8Contents]: <0a 14 1e>, byteLength:3 } } Value is a View:true Value is a DataView:true
参考: https://nodejs.org/api/util.html#util_util_types_isdataview_value
相关用法
注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Node.js | util.types.isDataView() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。