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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。