📜  Node.js | util.types.isDataView() 方法

📅  最后修改于: 2022-05-13 01:56:27.318000             🧑  作者: Mango

Node.js | util.types.isDataView() 方法

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);


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: 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:

节点.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