ArrayBuffer

ArrayBuffer 对象用于表示通用的原始二进制数据缓冲区。

¥The ArrayBuffer object is used to represent a generic raw binary data buffer.

它是一个字节数组,在其他语言中通常称为 "字节数组"。你不能直接操作 ArrayBuffer 的内容;相反,你创建 类型化数组对象DataView 对象之一以特定格式表示缓冲区,并使用它来读取和写入缓冲区的内容。

¥It is an array of bytes, often referred to in other languages as a "byte array". You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.

ArrayBuffer() 构造函数创建一个给定长度(以字节为单位)的新 ArrayBuffer。你还可以从现有数据获取数组缓冲区,例如从 Base64 字符串或 从本地文件

¥The ArrayBuffer() constructor creates a new ArrayBuffer of the given length in bytes. You can also get an array buffer from existing data, for example, from a Base64 string or from a local file.

ArrayBuffer可转让物品

¥ArrayBuffer is a transferable object.

描述

¥Description

调整 ArrayBuffer 的大小

¥Resizing ArrayBuffers

可以通过在调用 ArrayBuffer() 构造函数时包含 maxByteLength 选项来调整 ArrayBuffer 对象的大小。你可以通过分别访问 resizablemaxByteLength 属性来查询 ArrayBuffer 是否可调整大小以及其最大大小是多少。你可以通过 resize() 调用将新大小分配给可调整大小的 ArrayBuffer。新字节被初始化为 0。

¥ArrayBuffer objects can be made resizable by including the maxByteLength option when calling the ArrayBuffer() constructor. You can query whether an ArrayBuffer is resizable and what its maximum size is by accessing its resizable and maxByteLength properties, respectively. You can assign a new size to a resizable ArrayBuffer with a resize() call. New bytes are initialized to 0.

这些功能使调整 ArrayBuffer 的大小更加有效 - 否则,你必须使用新的大小制作缓冲区的副本。在这方面,它还使 JavaScript 与 WebAssembly 同等(Wasm 线性内存可以使用 WebAssembly.Memory.prototype.grow() 调整大小)。

¥These features make resizing ArrayBuffers more efficient — otherwise, you have to make a copy of the buffer with a new size. It also gives JavaScript parity with WebAssembly in this regard (Wasm linear memory can be resized with WebAssembly.Memory.prototype.grow()).

传输 ArrayBuffer

¥Transferring ArrayBuffers

可以使用 结构化克隆算法.ArrayBuffer 对象在不同的执行上下文(如 Web 工作线程服务工作进程)之间传输。这是通过在调用 Worker.postMessage()ServiceWorker.postMessage() 时将 ArrayBuffer 作为 可转让物品 传递来完成的。在纯 JavaScript 中,你还可以使用 transfer()transferToFixedLength() 方法将内存所有权从一个 ArrayBuffer 转移到另一个 ArrayBuffer

¥ArrayBuffer objects can be transferred between different execution contexts, like Web Workers or Service Workers, using the structured clone algorithm. This is done by passing the ArrayBuffer as a transferable object in a call to Worker.postMessage() or ServiceWorker.postMessage(). In pure JavaScript, you can also transfer the ownership of memory from one ArrayBuffer to another using its transfer() or transferToFixedLength() method.

ArrayBuffer 被转移时,其原始副本将被分离 - 这意味着它不再可用。在任何时刻,只有一个 ArrayBuffer 的副本实际上可以访问底层内存。分离的缓冲区具有以下行为:

¥When an ArrayBuffer is transferred, its original copy becomes detached — this means it is no longer usable. At any moment, there will only be one copy of the ArrayBuffer that actually has access to the underlying memory. Detached buffers have the following behaviors:

  • byteLength 变为 0(在缓冲区和关联的类型化数组视图中)。
  • 诸如 resize()slice() 之类的方法在调用时会抛出 TypeError。关联的类型化数组视图的方法也会抛出 TypeError

你可以检查 ArrayBuffer 是否通过其 detached 属性分离。

¥You can check whether an ArrayBuffer is detached by its detached property.

构造函数

¥Constructor

ArrayBuffer()

创建一个新的 ArrayBuffer 对象。

静态属性

¥Static properties

ArrayBuffer[@@species]

用于创建派生对象的构造函数。

静态方法

¥Static methods

ArrayBuffer.isView()

如果 arg 是 ArrayBuffer 视图之一,例如 类型化数组对象DataView,则返回 true。否则返回 false

实例属性

¥Instance properties

这些属性在 ArrayBuffer.prototype 上定义并由所有 ArrayBuffer 实例共享。

¥These properties are defined on ArrayBuffer.prototype and shared by all ArrayBuffer instances.

ArrayBuffer.prototype.byteLength

ArrayBuffer 的大小(以字节为单位)。这是在构造数组时确定的,并且如果 ArrayBuffer 的大小可调整,则只能使用 ArrayBuffer.prototype.resize() 方法进行更改。

ArrayBuffer.prototype.constructor

创建实例对象的构造函数。对于 ArrayBuffer 实例,初始值为 ArrayBuffer 构造函数。

ArrayBuffer.prototype.detached

只读。如果 ArrayBuffer 已分离(转移),则返回 true,否则返回 false

ArrayBuffer.prototype.maxByteLength

ArrayBuffer 可以调整大小的只读最大长度(以字节为单位)。这是在构造数组时确定的并且无法更改。

ArrayBuffer.prototype.resizable

只读。如果 ArrayBuffer 可以调整大小,则返回 true,否则返回 false

ArrayBuffer.prototype[@@toStringTag]

@@toStringTag 属性的初始值为字符串 "ArrayBuffer"。该属性在 Object.prototype.toString() 中使用。

实例方法

¥Instance methods

ArrayBuffer.prototype.resize()

ArrayBuffer 的大小调整为指定大小(以字节为单位)。

ArrayBuffer.prototype.slice()

返回一个新的 ArrayBuffer,其内容是该 ArrayBufferbegin(含)到 end(不包括)字节的副本。如果 beginend 为负数,则它指的是从数组末尾开始的索引,而不是从开头开始的索引。

ArrayBuffer.prototype.transfer()

创建一个与此缓冲区具有相同字节内容的新 ArrayBuffer,然后分离此缓冲区。

ArrayBuffer.prototype.transferToFixedLength()

创建一个新的不可调整大小的 ArrayBuffer,其字节内容与此缓冲区相同,然后分离此缓冲区。

示例

¥Examples

创建一个 ArrayBuffer

¥Creating an ArrayBuffer

在此示例中,我们创建一个 8 字节缓冲区,并使用引用该缓冲区的 Int32Array 视图:

¥In this example, we create a 8-byte buffer with a Int32Array view referring to the buffer:

js
const buffer = new ArrayBuffer(8);
const view = new Int32Array(buffer);

规范

Specification
ECMAScript Language Specification
# sec-arraybuffer-objects

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看