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

语法

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  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Basic supportChrome Full support 7Edge Full support YesFirefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 11.6Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
ArrayBuffer() without new throwsChrome Full support YesEdge Full support YesFirefox Full support 44IE No support NoOpera Full support YesSafari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android Full support 44Opera Android ? Safari iOS ? Samsung Internet Android ? nodejs Full support 0.12
byteLengthChrome Full support 7Edge Full support YesFirefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 11.6Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
isViewChrome Full support YesEdge Full support YesFirefox Full support 29IE Full support 11Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 29Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
prototypeChrome Full support 7Edge Full support YesFirefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 11.6Safari iOS Full support 4.2Samsung Internet Android Full support Yesnodejs Full support Yes
sliceChrome Full support YesEdge Full support YesFirefox 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 11Opera Full support YesSafari Full support 6WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox 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 YesSafari iOS Full support 6Samsung Internet Android Full support Yesnodejs Full support Yes
transfer
ExperimentalNon-standard
Chrome No support NoEdge No support NoFirefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
@@speciesChrome ? Edge ? Firefox Full support 48IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android Full support 48Opera Android ? Safari iOS ? Samsung Internet Android ? 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 对象需要用 new 运算符创建。如果调用构造函数时没有使用 new,将会抛出 TypeError  异常。

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

相关链接

文档标签和贡献者

最后编辑者: Jiang-Xuan,