util模塊的util.types.isSharedArrayBuffer()方法主要旨在滿足Node.js自己的內部API的需求。它用於檢查方法中傳遞的實例是否為內置 SharedArrayBuffer實例。
用法:
util.types.isSharedArrayBuffer( value )
參數:此方法接受單個參數值,該參數值包含任何值,即任何模塊的實例。
返回值:此方法返回一個布爾值,即,如果傳遞的值是SharedArrayBuffer的實例,則為true,否則返回false。
以下示例說明了Node.js中util.types.isSharedArrayBuffer()方法的使用:
範例1:
// Node.js program to demonstrate the
// util.types.isSharedArrayBuffer() method
// It includes util module
const util = require('util');
// Return true as passed instance
// is of SharedArrayBuffer
console.log(util.types.isSharedArrayBuffer(
new SharedArrayBuffer()));
// Return false as passed instance
// is of Int32Array
console.log(util.types.isSharedArrayBuffer(
new Int32Array()));
// Return false as the passed instance is
// of ArrayBuffer, not SharedArrayBuffer
console.log(util.types.isSharedArrayBuffer(
new ArrayBuffer()));
輸出:
true false false
範例2:
// Node.js program to demonstrate the
// util.types.isSharedArrayBuffer() method
// It includes util module
const util = require('util');
// making an instance of SharedArrayBuffer
// of size 1024
var sab = new SharedArrayBuffer(1024);
// Return true as passed instance is
// of SharedArrayBuffer
console.log(util.types.isSharedArrayBuffer(sab));
// Return true as slice method return new
// instance of sharedArrayBuffer
console.log(util.types.isSharedArrayBuffer(
sab.slice(100, 199)));
輸出:
true true
參考: https://nodejs.org/api/util.html#util_util_types_issharedarraybuffer_value
相關用法
注:本文由純淨天空篩選整理自akshajjuneja9大神的英文原創作品 Node.js | util.types.isSharedArrayBuffer() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。