SharedArrayBuffer.prototype.slice()

SharedArrayBuffer 实例的 slice() 方法返回一个新的 SharedArrayBuffer,其内容是该 SharedArrayBuffer 的字节的副本,从 start(含)到 end(不包括)。如果 startend 为负数,则它指的是从数组末尾开始的索引,而不是从开头开始的索引。

¥The slice() method of SharedArrayBuffer instances returns a new SharedArrayBuffer whose contents are a copy of this SharedArrayBuffer's bytes from start, inclusive, up to end, exclusive. If either start or end is negative, it refers to an index from the end of the array, as opposed to from the beginning.

Try it

语法

¥Syntax

js
slice()
slice(start)
slice(start, end)

参数

¥Parameters

start Optional

开始提取的从零开始的索引,转换为整数

  • 负索引从缓冲区末尾开始倒数 — 如果使用 -buffer.length <= start < 0start + buffer.length
  • 如果省略 start < -buffer.lengthstart,则使用 0
  • 如果是 start >= buffer.length,则不会提取任何内容。
end Optional

结束提取的从零开始的索引,转换为整数slice() 提取直至但不包括 end

  • 负索引从缓冲区末尾开始倒数 — 如果使用 -buffer.length <= end < 0end + buffer.length
  • 如果使用 end < -buffer.length0
  • 如果省略 end >= buffer.lengthend,则使用 buffer.length,导致提取直到末尾的所有元素。
  • 如果 end 暗示的位置在 start 暗示的位置之前或处,则不会提取任何内容。

返回值

¥Return value

包含提取元素的新 SharedArrayBuffer

¥A new SharedArrayBuffer containing the extracted elements.

示例

¥Examples

使用切片()

¥Using slice()

js
const sab = new SharedArrayBuffer(1024);
sab.slice(); // SharedArrayBuffer { byteLength: 1024 }
sab.slice(2); // SharedArrayBuffer { byteLength: 1022 }
sab.slice(-2); // SharedArrayBuffer { byteLength: 2 }
sab.slice(0, 1); // SharedArrayBuffer { byteLength: 1 }

规范

Specification
ECMAScript Language Specification
# sec-sharedarraybuffer.prototype.slice

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看