pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Int8Array/Int8Array

-a490-390a1aebf6dd/airgap.js" >

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Int8Array() 构造函数

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

Int8Array() 构造函数用于创建 Int8Array 对象。除非显式提供初始化数据,否则其内容会被初始化为 0

语法

js
new Int8Array()
new Int8Array(length)
new Int8Array(typedArray)
new Int8Array(object)

new Int8Array(buffer)
new Int8Array(buffer, byteOffset)
new Int8Array(buffer, byteOffset, length)

备注:Int8Array() 只能通过 new 进行构造。如果尝试在没有使用 new 的情况下调用它,会抛出 TypeError 异常。

参数

参见 TypedArray

异常

参见 TypedArray

示例

创建 Int8Array 的不同方式

js
// 通过长度创建
const int8 = new Int8Array(2);
int8[0] = 42;
console.log(int8[0]); // 42
console.log(int8.length); // 2
console.log(int8.BYTES_PER_ELEMENT); // 1

// 通过数组创建
const x = new Int8Array([21, 31]);
console.log(x[1]); // 31

// 通过另一个 TypedArray 创建
const y = new Int8Array(x);
console.log(y[0]); // 21

// 通过 ArrayBuffer 创建
const buffer = new ArrayBuffer(8);
const z = new Int8Array(buffer, 1, 4);
console.log(z.byteOffset); // 1

// 通过可迭代对象创建
const iterable = (function* () {
  yield* [1, 2, 3];
})();
const int8FromIterable = new Int8Array(iterable);
console.log(int8FromIterable);
// Int8Array [1, 2, 3]

规范

规范
ECMAScript® 2027 Language Specification
# sec-typedarray-constructors

浏览器兼容性

参见

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy