當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js Buffer.kMaxLength用法及代碼示例

Buffer.kMaxLength屬性是緩衝區模塊中Buffer類的內置應用程序編程接口,用於設置和獲取單個緩衝區實例允許的最大長度。

用法:

const Buffer.kMaxLength

參數:此屬性可以用作獲取器和設置器,因此有時會使用整數值作為參數。

返回值:此屬性返回單個緩衝區實例允許的最大長度。

範例1: 文件名:index.js



Javascript

// Node.js program to demonstrate the 
// Buffer.kMaxLength property 
  
// Creating and initializing arraybuffer object 
const arrbuff = new ArrayBuffer(16); 
  
// Getting buffer object form existing 
// arraybuffer object 
const buffer = Buffer.from(arrbuff); 
  
// Setting the the maximum value 
// into the buffer  
buffer.kMaxLength = 23; 
  
// Getting the maximum length by using 
// kMaxLength property 
const value = buffer.kMaxLength; 
  
// Disply the result 
console.log("kMaxLength is:" + value);

輸出:

kMaxLength is:23

範例2: 文件名:index.js

如果未初始化kMaxLength

Javascript

// Node.js program to demonstrate the 
// Buffer.kMaxLength property 
  
// If kMaxLengthis is not initialized 
  
// Creating and initializing arraybuffer 
// object 
const arrbuff = new ArrayBuffer(16); 
  
// Getting buffer object form existing 
// arraybuffer object 
const buffer = Buffer.from(arrbuff); 
  
// Getting the maximum length by using 
// kMaxLength property 
const value = buffer.kMaxLength; 
  
// Disply the result 
console.log("kMaxLength is:" + value);

輸出:

kMaxLengthis is:undefined

使用以下命令運行index.js文件:

node index.js

參考:https://nodejs.org/dist/latest-v12.x/docs/api/buffer.html#buffer_buffer_kmaxlength

相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js Buffer.kMaxLength Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。