ArrayBuffer() constructor is used to create ArrayBuffer 对象。

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

The size, in bytes, of the array buffer to create.

返回值

A new ArrayBuffer object of the specified size. Its contents are initialized to 0.

异常

A RangeError is thrown if the length is larger than Number.MAX_SAFE_INTEGER (>= 2 ** 53) or negative.

Compatibility notes

Starting with ECMAScript 2015, ArrayBuffer constructors require to be constructed with a new operator. Calling an ArrayBuffer constructor as a function without new , will throw a TypeError from now on.

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

范例

Creating an ArrayBuffer

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

var buffer = new ArrayBuffer(8);
var view   = new Int32Array(buffer);
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'ArrayBuffer' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 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 7 Edge 12 Firefox 4 IE 10 Opera 11.6 Safari 5.1 WebView Android 4 Chrome Android 18 Firefox Android 4 Opera Android 12 Safari iOS 4.2 Samsung Internet Android 1.0 nodejs 0.10
ArrayBuffer() without new throws Chrome 7 Edge 14 Firefox 44 IE No Opera 15 Safari 5.1 WebView Android ≤37 Chrome Android 18 Firefox Android 44 Opera Android 14 Safari iOS 5 Samsung Internet Android 1.0 nodejs 0.12

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: