subarray() 返回一个新的、基于相同 ArrayBuffer、元素类型也相同的的 TypedArray。开始的索引将会被包括,而结束的索引将不会被包括。TypedArray 是指 typed array types 的其中之一。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
typedarray.subarray([begin [,end]])
begin 所指定的那一个元素到数组末尾的所有元素都将会被包含进新数组中。一个新的 TypedArray 对象。
begin 和 end 指定的范围将会收束与当前数组的有效索引begin 或 end 是负数,将会被当做成是从数组末端读取的索引需要注意的是,使用该方法返回的新数组还是建立在原有的 Buffer 之上的,所以,改动数组的内容将会影响到原数组,反之亦然。
subarray methodvar buffer = new ArrayBuffer(8);
var uint8 = new Uint8Array(buffer);
uint8.set([1,2,3]);
console.log(uint8); // Uint8Array [ 1, 2, 3, 0, 0, 0, 0, 0 ]
var sub = uint8.subarray(0,4);
console.log(sub); // Uint8Array [ 1, 2, 3, 0 ]
| Specification | Status | Comment |
|---|---|---|
| Typed Array Specification | Obsolete | Superseded by ECMAScript 6. |
| ECMAScript 2015 (6th Edition, ECMA-262) TypedArray.prototype.subarray |
Standard | Initial definition in an ECMA standard. |
| ECMAScript Latest Draft (ECMA-262) TypedArray.prototype.subarray |
Draft |
| Desktop | Mobile | Server | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
subarray | Chrome Full support 7 | Edge Full support 14 | Firefox Full support 4 | IE Full support 10 | Opera Full support 11.6 | Safari Full support 5.1 | WebView Android Full support 4 | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support 11.6 | Safari iOS Full support 4.2 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |