buf.slice([start[, end]])
曆史
| 版本 | 變化 | 
|---|---|
| v17.5.0 | buf.slice() 方法已被棄用。  | 
| v7.0.0 | 現在,所有偏移量在對其進行任何計算之前都被強製轉換為整數。  | 
| v7.1.0、v6.9.2 | 將偏移量強製為整數現在可以正確處理 32 位整數範圍之外的值。  | 
| v0.3.0 | 添加於:v0.3.0  | 
參數
start<integer> 新的Buffer將從哪裏開始。 默認:0。end<integer> 新的Buffer將在哪裏結束(不包括在內)。 默認:。buf.length- 返回: <Buffer>
 
Stability: 0 - 已棄用:改用 
 buf.subarray 。返回一個新的 Buffer,它引用與原始內存相同的內存,但被 start 和 end 索引偏移和裁剪。
此方法與 Uint8Array.prototype.slice() 不兼容,它是 Buffer 的超類。要複製切片,請使用 Uint8Array.prototype.slice() 。
import { Buffer } from 'node:buffer'; const buf = Buffer.from('buffer'); const copiedBuf = Uint8Array.prototype.slice.call(buf); copiedBuf[0]++; console.log(copiedBuf.toString()); // Prints: cuffer console.log(buf.toString()); // Prints: buffer // With buf.slice(), the original buffer is modified. const notReallyCopiedBuf = buf.slice(); notReallyCopiedBuf[0]++; console.log(notReallyCopiedBuf.toString()); // Prints: cuffer console.log(buf.toString()); // Also prints: cuffer (!)const { Buffer } = require('node:buffer'); const buf = Buffer.from('buffer'); const copiedBuf = Uint8Array.prototype.slice.call(buf); copiedBuf[0]++; console.log(copiedBuf.toString()); // Prints: cuffer console.log(buf.toString()); // Prints: buffer // With buf.slice(), the original buffer is modified. const notReallyCopiedBuf = buf.slice(); notReallyCopiedBuf[0]++; console.log(notReallyCopiedBuf.toString()); // Prints: cuffer console.log(buf.toString()); // Also prints: cuffer (!)
相關用法
- Node.js Buffer buf.swap32()用法及代碼示例
 - Node.js Buffer buf.swap64()用法及代碼示例
 - Node.js Buffer buf.swap16()用法及代碼示例
 - Node.js Buffer buf.subarray([start[, end]])用法及代碼示例
 - Node.js Buffer buf.writeBigUInt64BE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.toString([encoding[, start[, end]]])用法及代碼示例
 - Node.js Buffer buf.writeDoubleLE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.writeBigInt64LE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.keys()用法及代碼示例
 - Node.js Buffer buf.writeFloatLE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.indexOf(value[, byteOffset][, encoding])用法及代碼示例
 - Node.js Buffer buf.readFloatLE([offset])用法及代碼示例
 - Node.js Buffer buf.readInt32LE([offset])用法及代碼示例
 - Node.js Buffer buf.writeInt8(value[, offset])用法及代碼示例
 - Node.js Buffer buf.writeInt32LE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.writeIntLE(value, offset, byteLength)用法及代碼示例
 - Node.js Buffer buf.values()用法及代碼示例
 - Node.js Buffer buf.writeDoubleBE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.length用法及代碼示例
 - Node.js Buffer buf.byteOffset用法及代碼示例
 - Node.js Buffer buf.readUInt32BE([offset])用法及代碼示例
 - Node.js Buffer buf.readDoubleLE([offset])用法及代碼示例
 - Node.js Buffer buf.writeFloatBE(value[, offset])用法及代碼示例
 - Node.js Buffer buf.readBigUInt64LE([offset])用法及代碼示例
 - Node.js Buffer buf.readUInt16BE([offset])用法及代碼示例
 
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 buf.slice([start[, end]])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
