Int8Array 类型数组表示二进制补码 8 位有符号整数的数组。内容初始化为 0。一旦建立,你可以使用对象的方法引用数组中的元素,或使用标准数组索引语法 ( 即,使用括号注释)。

语法

new Int8Array(length);
new Int8Array(typedArray);
new Int8Array(object);
new Int8Array(buffer [, byteOffset [, length]]);

有关构造函数语法和参数的更多信息,请访问 TypedArray.

静态属性

  • {{jsxref("TypedArray.BYTES_PER_ELEMENT", "Int8Array.BYTES_PER_ELEMENT")}}
    • : 返回数组中每个元素的大小。在 Int8Array 中这个值为 1。
  • Int8Array.length
    • : 此属性为固定值属性,值为 3。查看 {{jsxref("TypedArray.prototype.length", "Int8Array.prototype.length")}} 获得获取数组内元素个数方法。
  • {{jsxref("TypedArray", "Int8Array.prototype")}}
    • : TypedArray 对象的构造原型。

静态方法

  • {{jsxref("TypedArray.from", "Int8Array.from()")}}
    • : 从类数组对象或迭代器生成 int8Array 数组对象。参照 Array.from()
  • {{jsxref("TypedArray.of", "Int8Array.of()")}}
    • : 以多个参数构造 Int8Array 对象,参照 Array.of()

实例属性

还从其父接口 TypedArray 继承实例属性。

  • Int8Array.prototype.constructor
    • : 这个方法会返回对象的构造原型。默认为Int8Array 构造函数。
  • {{jsxref("TypedArray.prototype.buffer", "Int8Array.prototype.buffer")}} {{readonlyInline}}
    • : Returns the ArrayBuffer referenced by the Int8Array Fixed at construction time and thus read only.
  • {{jsxref("TypedArray.prototype.byteLength", "Int8Array.prototype.byteLength")}} {{readonlyInline}}
    • : Returns the length (in bytes) of the Int8Array from the start of its ArrayBuffer. Fixed at construction time and thus read only.
  • {{jsxref("TypedArray.prototype.byteOffset", "Int8Array.prototype.byteOffset")}} {{readonlyInline}}
    • : Returns the offset (in bytes) of the Int8Array from the start of its ArrayBuffer. Fixed at construction time and thus read only.
  • {{jsxref("TypedArray.prototype.length", "Int8Array.prototype.length")}} {{readonlyInline}}
    • : 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);