buf.slice([start[, end]])


版本历史
版本变更
v17.5.0, v16.15.0

buf.slice() 方法已被弃用。

v7.0.0

在进行任何计算之前,所有偏移量现在都被强制为整数。

v7.1.0, v6.9.2

将偏移量强制为整数现在可以正确处理 32 位整数范围之外的值。

v0.3.0

新增于: v0.3.0

稳定性: 0 - 已弃用:改用 buf.subarray

\Stability: 0 - Deprecated: Use buf.subarray instead.

返回一个新的 Buffer,它引用与原始内存相同的内存,但由 startend 索引进行偏移和裁剪。

\Returns a new Buffer that references the same memory as the original, but offset and cropped by the start and end indexes.

此方法与 Uint8Array.prototype.slice()(Buffer 的超类)不兼容。要复制切片,则使用 Uint8Array.prototype.slice()

\This method is not compatible with the Uint8Array.prototype.slice(), which is a superclass of Buffer. To copy the slice, use 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 (!)

AltStyle によって変換されたページ (->オリジナル) /