Int8Array
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.
Int8Array
类型数组表示二进制补码 8 位有符号整数的数组。内容初始化为 0。一旦建立,你可以使用对象的方法引用数组中的元素,或使用标准数组索引语法 ( 即,使用括号注释)。
语法
new Int8Array(length); new Int8Array(typedArray); new Int8Array(object); new Int8Array(buffer [, byteOffset [, length]]);
有关构造函数语法和参数的更多信息,请访问 TypedArray.
静态属性
Int8Array.BYTES_PER_ELEMENT
返回数组中每个元素的大小。在
Int8Array
中这个值为 1。- Int8Array.length
此属性为固定值属性,值为 3。查看
Int8Array.prototype.length
获得获取数组内元素个数方法。Int8Array.prototype
TypedArray 对象的构造原型。
静态方法
Int8Array.from()
从类数组对象或迭代器生成 int8Array 数组对象。参照
Array.from()
Int8Array.of()
以多个参数构造 Int8Array 对象,参照
Array.of()
实例属性
还从其父接口 TypedArray
继承实例属性。
Int8Array.prototype.constructor
这个方法会返回对象的构造原型。默认为
Int8Array
构造函数。Int8Array.prototype.buffer
只读Returns the
ArrayBuffer
referenced by theInt8Array
Fixed at construction time and thus read only.Int8Array.prototype.byteLength
只读Returns the length (in bytes) of the
Int8Array
from the start of itsArrayBuffer
. Fixed at construction time and thus read only.Int8Array.prototype.byteOffset
只读Returns the offset (in bytes) of the
Int8Array
from the start of itsArrayBuffer
. Fixed at construction time and thus read only.Int8Array.prototype.length
只读Returns the number of elements hold in the
Int8Array
. Fixed at construction time and thus read only.
实例方法
从其父接口 TypedArray
继承实例方法。
示例
// 以长度参数构造对象 var int8 = new Int8Array(2); int8[0] = 42; console.log(int8[0]); // 42 console.log(int8.length); // 2 console.log(int8.BYTES_PER_ELEMENT); // 1 // 以数组构造对象 var arr = new Int8Array([21, 31]); console.log(arr[1]); // 31 // 从另一数组构造对象 var x = new Int8Array([21, 31]); var y = new Int8Array(x); console.log(y[0]); // 21 // 从 ArrayBuffer 构造对象 var buffer = new ArrayBuffer(8); var z = new Int8Array(buffer, 1, 4);
规范
Specification |
---|
ECMAScript® 2026 Language Specification # table-49 |