ArrayBuffer 物件是一種表示通用、固定大小的原始二進制資料緩衝。想要直接操作一個 ArrayBuffer 物件的內容是不可能的。若要讀寫該緩衝的內容則必須透過視圖,可以選擇建立一個 DataView 視圖物件或是一個限定其成員為某種型別的 TypedArray 視圖物件,它們皆能以特定的型別解讀、修改 ArrayBuffer。
語法
new ArrayBuffer(length)
參數
length- 要建立的緩衝陣列大小,以位元組(byte)計算。
回傳值
為一個新建立的指定大小 ArrayBuffer 物件,其內容皆初始化為 0。
說明
The ArrayBuffer constructor creates a new ArrayBuffer of the given length in bytes.
從既有的資料取得 ArrayBuffer
屬性
ArrayBuffer.length- The
ArrayBufferconstructor's length property whose value is 1. get ArrayBuffer[@@species]- The constructor function that is used to create derived objects.
ArrayBuffer.prototype- Allows the addition of properties to all
ArrayBufferobjects.
方法
ArrayBuffer.isView(arg)- Returns
trueifargis one of the ArrayBuffer views, such astyped array objectsor aDataView. Returnsfalseotherwise. ArrayBuffer.transfer(oldBuffer [, newByteLength])-
Returns a new
ArrayBufferwhose contents are taken from theoldBuffer's data and then is either truncated or zero-extended bynewByteLength.
ArrayBuffer 實例
所有的 ArrayBuffer 物件實例皆繼承自 ArrayBuffer.prototype.
屬性
- ArrayBuffer.prototype.constructor
- Specifies the function that creates an object's prototype. The initial value is the standard built-in
ArrayBufferconstructor. ArrayBuffer.prototype.byteLengthRead only- 陣列大小,以位元組(byte)計算。此屬性在陣列建立之後就不可能改變了。唯讀。
方法
ArrayBuffer.prototype.slice()- Returns a new
ArrayBufferwhose contents are a copy of thisArrayBuffer's bytes frombegin, inclusive, up toend, exclusive. If eitherbeginorendis negative, it refers to an index from the end of the array, as opposed to from the beginning.
範例
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);
規範
| Specification | Status | Comment |
|---|---|---|
| Typed Array Specification | Obsolete | Superseded by ECMAScript 6. |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'ArrayBuffer' in that specification. |
Standard | Initial definition in an ECMA standard. Specified that new is required. |
| ECMAScript 2017 Draft (ECMA-262) The definition of 'ArrayBuffer' in that specification. |
Draft |
瀏覽器相容性
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 7.0 | 4.0 (2) | 10 | 11.6 | 5.1 |
ArrayBuffer() without new throws |
? | 44 (44) | ? | ? | ? |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | 4.0 | (Yes) | 4.0 (2) | 10 | 11.6 | 4.2 |
ArrayBuffer() without new throws |
? | ? | 44.0 (44) | ? | ? | ? |
相容性備註
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);