大网数据广告活动
Web 标准与前端开发

Int8Array

· 52CE 我爱测

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

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 Int8Array Fixed at construction time and thus read only.
  • TypedArray.prototype.byteLength
    • : Returns the length (in bytes) of the Int8Array from the start of its ArrayBuffer. Fixed at construction time and thus read only.
  • TypedArray.prototype.byteOffset
    • : Returns the offset (in bytes) of the Int8Array from the start of its ArrayBuffer. Fixed at construction time and thus read only.
  • TypedArray.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);

规范

浏览器兼容性

兼容性说明

自 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]);

参见

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。