TypedArray.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 2016年9月.
TypedArray 实例的 slice() 方法返回一个新的类型化数组对象,包含原类型化数组中从 start 到 end(不包括 end)索引位置的一部分拷贝。start 和 end 参数表示要提取的项的索引。原始类型化数组不会被修改。此方法的算法与 Array.prototype.slice() 相同。
尝试一下
const bytes = new Uint8Array([10, 20, 30, 40, 50]);
const byteSlice = bytes.slice(1, 3);
console.log(byteSlice);
// 预期输出:Uint8Array [20, 30]
语法
js
slice()
slice(start)
slice(start, end)
参数
返回值
一个包含提取元素的新类型化数组。
描述
更多详细信息请参见 Array.prototype.slice()。此方法不是通用方法,只能在类型化数组实例上调用。
示例
>返回现有类型化数组的一部分
js
const bytes = new Uint8Array([1, 2, 3]);
bytes.slice(1); // Uint8Array [ 2, 3 ]
bytes.slice(2); // Uint8Array [ 3 ]
bytes.slice(-2); // Uint8Array [ 2, 3 ]
bytes.slice(0, 1); // Uint8Array [ 1 ]
规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-%typedarray%.prototype.slice> |