Int8Array 类型数组表示二进制补码 8 位有符号整数的数组。内容初始化为 0。一旦建立,你可以使用对象的方法引用数组中的元素,或使用标准数组索引语法 ( 即,使用括号注释)。
语法
new Int8Array(length);
new Int8Array(typedArray);
new Int8Array(object);
new Int8Array(buffer [, byteOffset [, length]]);
有关构造函数语法和参数的更多信息,请访问 TypedArray.
静态属性
- TypedArray.BYTES_PER_ELEMENT
- : 返回数组中每个元素的大小。在
Int8Array中这个值为 1。
- : 返回数组中每个元素的大小。在
- Int8Array.length
- : 此属性为固定值属性,值为 3。查看 TypedArray.prototype.length 获得获取数组内元素个数方法。
- TypedArray
- : TypedArray 对象的构造原型。
静态方法
- TypedArray.from
- : 从类数组对象或迭代器生成 int8Array 数组对象。参照 Array.from()
- TypedArray.of
- : 以多个参数构造 Int8Array 对象,参照 Array.of()
实例属性
还从其父接口 TypedArray 继承实例属性。
Int8Array.prototype.constructor- : 这个方法会返回对象的构造原型。默认为
Int8Array构造函数。
- : 这个方法会返回对象的构造原型。默认为
- TypedArray.prototype.buffer
- : Returns the ArrayBuffer referenced by the
Int8ArrayFixed at construction time and thus read only.
- : Returns the ArrayBuffer referenced by the
- TypedArray.prototype.byteLength
- : Returns the length (in bytes) of the
Int8Arrayfrom the start of its ArrayBuffer. Fixed at construction time and thus read only.
- : Returns the length (in bytes) of the
- TypedArray.prototype.byteOffset
- : Returns the offset (in bytes) of the
Int8Arrayfrom the start of its ArrayBuffer. Fixed at construction time and thus read only.
- : Returns the offset (in bytes) of the
- TypedArray.prototype.length
- : Returns the number of elements hold in the
Int8Array. Fixed at construction time and thus read only.
- : Returns the number of elements hold in the
实例方法
从其父接口 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);
规范
浏览器兼容性
兼容性说明
自 ECMAScript 2015 (ES6) 施行,Int8Array 需要使用 new 构造。从当前版本开始,不加 new 而便调用 Int8Array 构造器方法,将报出 TypeError 错误。
var dv = Int8Array([1, 2, 3]);
// TypeError: calling a builtin Int8Array constructor
// without new is forbidden
var dv = new Int8Array([1, 2, 3]);
参见
core-js中Int8Array的 polyfill- JavaScript 类型化数组指南
- TypedArray
- ArrayBuffer
- DataView
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
