ArrayBuffer.prototype.slice()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
ArrayBuffer
实例的 slice()
方法返回一个新的 ArrayBuffer
实例,其包含原 ArrayBuffer
实例中从 begin
开始(包含)到 end
结束(不含)的所有字节的副本。
尝试一下
// Create an ArrayBuffer with a size in bytes
const buffer = new ArrayBuffer(16);
const int32View = new Int32Array(buffer);
// Produces Int32Array [0, 0, 0, 0]
int32View[1] = 42;
const sliced = new Int32Array(buffer.slice(4, 12));
// Produces Int32Array [42, 0]
console.log(sliced[0]);
// Expected output: 42
语法
js
slice()
slice(start)
slice(start, end)
参数
start
可选-
要开始提取的位置索引(从 0 开始),将被转换为整数。
- 负数索引将会从缓冲区末尾开始计算——如果
start < 0
,那么将会使用start + buffer.length
。 - 如果
start < -buffer.length
或省略了start
,则会使用0
。 - 如果
start >= buffer.length
,则不会提取任何内容。
- 负数索引将会从缓冲区末尾开始计算——如果
end
可选-
要结束提取的位置索引(从 0 开始),将被转换为整数。
slice()
提取到但不包括end
。- 负数索引将会从缓冲区末尾开始计算——如果
end < 0
,那么将会使用end + buffer.length
。 - 如果
end < -buffer.length
,则会使用0
。 - 如果
end >= buffer.length
或省略了end
,则会使用buffer.length
,则会导致直到末尾的所有元素都被提取。 - 如果标准化后的
end
位置在start
位置之前,则不会提取任何内容。
- 负数索引将会从缓冲区末尾开始计算——如果
返回值
一个新的 ArrayBuffer
对象。
示例
复制一个 ArrayBuffer
js
const buf1 = new ArrayBuffer(8);
const buf2 = buf1.slice(0);
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-arraybuffer.prototype.slice |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
slice |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- See implementation notes.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.