ArrayBuffer は、一般的な固定長のバイナリデータのバッファを示すために使われるデータタイプです。ArrayBuffer の内容物を直接操作することはできません。代わりにバッファを特定の形式で表現し、またバッファの内容物を読み書きするために使用される typed array object か DataView オブジェクトを作成します。
構文
new ArrayBuffer(length)
パラメーター
length- ArrayBuffer を生成するためのバイト単位のサイズ。
戻り値
指定したサイズの新しい ArrayBuffer オブジェクト。内容物は 0 に初期化される。
説明
ArrayBuffer コンストラクターは、与えられたバイト単位の長さの新しい ArrayBuffer を生成します。
既存のデータから ArrayBuffer を取得する
プロパティ
ArrayBuffer.lengthArrayBufferコンストラクターの length プロパティの値は 1。get ArrayBuffer[@@species]- 派生オブジェクトを生成するために使用されるコンストラクター関数。
ArrayBuffer.prototype- すべての
ArrayBufferオブジェクトへのプロパティ追加できる。
メソッド
ArrayBuffer.isView(arg)argが TypedArray オブジェクト かDataViewのいずれかのような ArrayBuffer view 場合、trueを返す。そうでなければfalseを返す。ArrayBuffer.transfer(oldBuffer [, newByteLength])-
内容物が
oldBufferのデータから取られて、newByteLengthによって切り捨てられるか、ゼロ拡張された新しいArrayBufferを返す。
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- The size, in bytes, of the array. This is established when the array is constructed and cannot be changed. Read only.
メソッド
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.
例
この例では、バッファを参照する Int32Array view で 8 バイトのバッファを生成しています:
var buffer = new ArrayBuffer(8); var view = new Int32Array(buffer);
仕様
| 仕様 | ステータス | コメント |
|---|---|---|
| Typed Array Specification | 廃止された | ECMAScript 6 に取って代わられた。 |
| ECMAScript 2015 (6th Edition, ECMA-262) ArrayBuffer の定義 |
標準 | ECMA 標準の初期定義。new が必要なように指定された。 |
| ECMAScript 2017 Draft (ECMA-262) ArrayBuffer の定義 |
ドラフト |
ブラウザ実装状況
| 機能 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| 基本サポート | 7.0 | 4.0 (2) | 10 | 11.6 | 5.1 |
new がない場合、ArrayBuffer() が例外をスロー |
? | 44 (44) | ? | ? | ? |
| 機能 | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| 基本サポート | 4.0 | (有) | 4.0 (2) | 10 | 11.6 | 4.2 |
new がない場合、ArrayBuffer() が例外をスロー |
? | ? | 44.0 (44) | ? | ? | ? |
互換のための注記
ECMAScript 2015 から、ArrayBuffer コンストラクターは new 演算子を必要とするようになりました。new なしで関数として ArrayBuffer コンストラクターを呼び出すと、TypeError をスローするようになっています。
var dv = ArrayBuffer(10); // TypeError: calling a builtin ArrayBuffer constructor // without new is forbidden
var dv = new ArrayBuffer(10);