ArrayBuffer
对象用来表示通用的、固定长度的原始二进制数据缓冲区。ArrayBuffer
不能直接操作,而是要通过类型数组对象或 DataView
对象来操作,它们会将缓冲区中的数据表示为特定的格式,并通过这些格式来读写缓冲区的内容。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
语法
new ArrayBuffer(length)
参数
length
- 要创建的
ArrayBuffer
的大小,单位为字节。
返回值
一个指定大小的 ArrayBuffer
对象,其内容被初始化为 0。
异常
如果 length 大于 Number.MAX_SAFE_INTEGER
(>= 2 ** 53)或为负数,则抛出一个 RangeError
异常。
描述
ArrayBuffer
构造函数用来创建一个指定字节长度的 ArrayBuffer
对象。
以现有数据获取 ArrayBuffer
属性
ArrayBuffer.length
- ArrayBuffer 构造函数的 length 属性,其值为1。
get ArrayBuffer[@@species]
- 返回 ArrayBuffer 的构造函数。
ArrayBuffer.prototype
- 通过 ArrayBuffer 的原型对象可以为所有 ArrayBuffer 对象添加属性。
方法
ArrayBuffer.isView(arg)
- 如果参数是 ArrayBuffer 的视图实例则返回
true
,例如 类型数组对象 或DataView
对象;否则返回false
。 ArrayBuffer.transfer(oldBuffer [, newByteLength])
-
返回一个新的 ArrayBuffer 对象,其内容取自
oldBuffer
中的数据,并且根据newByteLength
的大小对数据进行截取或补 0。 ArrayBuffer.slice()
- 和
ArrayBuffer.prototype.slice()
功能相同。
ArrayBuffer 实例
所有 ArrayBuffer
实例都会从 ArrayBuffer.prototype
继承属性和方法。
属性
- ArrayBuffer.prototype.constructor
- 指定函数,它创建一个对象的原型。其初始值是标准ArrayBuffer内置构造函数。
ArrayBuffer.prototype.byteLength
只读- 数组的字节大小。在数组创建时确定,并且不可变更。只读。
方法
ArrayBuffer.prototype.slice()
- 返回一个新的
ArrayBuffer
,它的内容是这个ArrayBuffer
的字节副本,从begin(包括),到end(不包括)。如果begin或end是负数,则指的是从数组末尾开始的索引,而不是从头开始。
示例
下面的例子创建了一个 8 字节的缓冲区,并使用一个 Int32Array
来引用它:
var buffer = new ArrayBuffer(8);
var view = new Int32Array(buffer);
规范
Specification | Status | Comment |
---|---|---|
Typed Array Specification | Obsolete | 已被 ECMAScript 6 中的 ArrayBuffer 取代 |
ECMAScript 2015 (6th Edition, ECMA-262) ArrayBuffer |
Standard | 在 ECMA 标准中的初始定义。规定了必须通过 new 来调用构造函数 |
ECMAScript Latest Draft (ECMA-262) ArrayBuffer |
Draft |
浏览器兼容性
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ArrayBuffer | Chrome Full support 7 | Edge Full support 12 | Firefox Full support 4 | IE Full support 10 | Opera Full support 11.6 | Safari Full support 5.1 | WebView Android Full support 4 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 4.2 | Samsung Internet Android Full support Yes | nodejs Full support Yes |
byteLength | Chrome Full support 7 | Edge Full support 12 | Firefox Full support 4 | IE Full support 10 | Opera Full support 11.6 | Safari Full support 5.1 | WebView Android Full support 4 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 4.2 | Samsung Internet Android Full support Yes | nodejs Full support Yes |
isView | Chrome Full support 32 | Edge Full support 12 | Firefox Full support 29 | IE Full support 11 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support ≤37 | Chrome Android Full support 32 | Firefox Android Full support 29 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 2.0 | nodejs Full support Yes |
ArrayBuffer() without new throws | Chrome Full support 7 | Edge Full support 14 | Firefox Full support 44 | IE No support No | Opera Full support Yes | Safari Full support 5.1 | WebView Android Full support ≤37 | Chrome Android Full support 18 | Firefox Android Full support 44 | Opera Android Full support Yes | Safari iOS Full support 5 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.12 |
prototype | Chrome Full support 7 | Edge Full support 12 | Firefox Full support 4 | IE Full support 10 | Opera Full support 11.6 | Safari Full support 5.1 | WebView Android Full support 4 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 4.2 | Samsung Internet Android Full support Yes | nodejs Full support Yes |
slice | Chrome Full support 17 | Edge Full support 12 | Firefox
Full support
12
| IE Full support 11 | Opera Full support Yes | Safari Full support 6 | WebView Android Full support ≤37 | Chrome Android Full support 18 | Firefox Android
Full support
14
| Opera Android Full support Yes | Safari iOS Full support 6 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
transfer | Chrome No support No | Edge No support No | Firefox No support No | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android No support No | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
@@species | Chrome Full support 51 | Edge Full support 13 | Firefox Full support 48 | IE No support No | Opera Full support 38 | Safari ? | WebView Android Full support 51 | Chrome Android Full support 51 | Firefox Android Full support 48 | Opera Android Full support 41 | Safari iOS ? | Samsung Internet Android Full support 5.0 | nodejs
Full support
6.5.0
|
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- Experimental. Expect behavior to change in the future.
- Experimental. Expect behavior to change in the future.
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.
兼容性提醒
从 ECMAScript 2015 开始,ArrayBuffer
对象需要用 new
运算符创建。如果调用构造函数时没有使用 new
,将会抛出 TypeError
异常。
var dv = ArrayBuffer(10); // TypeError: calling a builtin ArrayBuffer constructor // without new is forbidden
var dv = new ArrayBuffer(10);