ArrayBuffer

ArrayBuffer 对象用来表示通用的、固定长度的原始二进制数据缓冲区。ArrayBuffer 不能直接操作,而是要通过DataView 对象来操作,它们会将缓冲区中的数据表示为特定的格式,并通过这些格式来读写缓冲区的内容。

语法

new ArrayBuffer(length)

参数

length
要创建的 ArrayBuffer 的大小,单位为字节。

返回值

一个指定大小的 ArrayBuffer 对象,其内容被初始化为 0。

异常

如果 length 大于 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  

浏览器兼容性

Update compatibility data on GitHub
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
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
Notes
Full support 12
Notes
Notes The non-standard ArrayBuffer.slice() method has been removed in Firefox 53 (but the standardized version ArrayBuffer.prototype.slice() is kept.
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
Notes
Full support 14
Notes
Notes The non-standard ArrayBuffer.slice() method has been removed in Firefox 53 (but the standardized version ArrayBuffer.prototype.slice() is kept.
Opera Android Full support Yes Safari iOS Full support 6 Samsung Internet Android Full support 1.0 nodejs Full support Yes
transfer
Experimental Non-standard
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
Full support 6.5.0
Full support 6.0.0
Disabled
Disabled From version 6.0.0: this feature is behind the --harmony runtime flag.

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 对象需要用 TypeError  异常。

var dv = ArrayBuffer(10);
// TypeError: calling a builtin ArrayBuffer constructor 
// without new is forbidden
var dv = new ArrayBuffer(10);

相关链接