L'oggetto ArrayBuffer viene utilizzato per rappresentare un buffer di lunghezza fissa di dati non elaborati. Non è possibile manipolare il contenuto di un ArrayBuffer, è possibile invece creare un array di oggetti tipizzati o un oggetto di tipo DataView che rappresenta il buffer in uno specifico formato ed utilizzare questo per leggere e scrivere il contenuto del buffer.
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.
Sintassi
new ArrayBuffer(length)
Parametri
length- La dimensione, in bytes, dell'array buffer da creare.
Valori di ritorno
Un nuovo oggetto di tipo ArrayBuffer della dimensione specificata. Il suo contenuto è inzializzato a 0.
Eccezioni
Un RangeError viene generato se la lunghezza è maggiore di di Number.MAX_SAFE_INTEGER (>= 2 ** 53) oppure è negativa.
Descrizione
Il costruttore ArrayBuffer crea un nuovo ArrayBuffer di lunghezza specificata in bytes.
Ottenere un array buffer da dati pre-esistenti
Proprietà
ArrayBuffer.length- La dimensione del costruttore
ArrayBufferil cui valore è 1. get ArrayBuffer[@@species]- La funzione costruttore utilizzata per creare oggetti derivati.
ArrayBuffer.prototype- Permette di aggiungere proprietà a tutti gli oggetti di tipo
ArrayBuffer.
Metodi
ArrayBuffer.isView(arg)- Restituisce
trueseargsè una delle viste di ArrayBuffer, come un array di oggetti tipizzato o unDataView. Altrimenti restituiscefalse. ArrayBuffer.transfer(oldBuffer [, newByteLength])- Restituisce un nuovo
ArrayBufferi cui contenuti sono presi dai dati dell'oggettooldBuffere successivamente troncato o esteso a zero tramitenewByteLength.
Istanze
Tutte le istanze di ArrayBuffer ereditano da ArrayBuffer.prototype.
Proprietà
Metodi
ArrayBuffer.slice()- Esegue le stesse operazioni di
ArrayBuffer.prototype.slice().
Esempio
In questo esempio viene creato un buffer di 8 byte con una vista di tipo Int32Array che si riferisce al buffer
var buffer = new ArrayBuffer(8); var view = new Int32Array(buffer);
Specifiche
| Specification | Status | Comment |
|---|---|---|
| Typed Array Specification | Obsolete | Sostituito da ECMAScript 6. |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'ArrayBuffer' in that specification. |
Standard |
Definizione iniziale con standard ECMA. Specifica che |
| ECMAScript (ECMA-262) The definition of 'ArrayBuffer' in that specification. |
Living Standard |
Browser compatibility
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 1.0 | nodejs Full support 0.10 |
ArrayBuffer() constructor | 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 1.0 | nodejs Full support 0.10 |
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 1.0 | nodejs Full support 0.10 |
isView | Chrome Full support 32 | Edge Full support 12 | Firefox Full support 29 | IE Full support 11 | Opera Full support 19 | Safari Full support 7 | WebView Android Full support ≤37 | Chrome Android Full support 32 | Firefox Android Full support 29 | Opera Android Full support 19 | Safari iOS Full support 7 | Samsung Internet Android Full support 2.0 | nodejs Full support 4.0.0 |
slice | Chrome Full support 17 | Edge Full support 12 | Firefox
Full support
12
| IE Full support 11 | Opera Full support 12.1 | Safari Full support 6 | WebView Android Full support ≤37 | Chrome Android Full support 18 | Firefox Android
Full support
14
| Opera Android Full support 12.1 | Safari iOS Full support 6 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.12 |
@@species | Chrome Full support 51 | Edge Full support 13 | Firefox Full support 48 | IE No support No | Opera Full support 38 | Safari Full support 10 | WebView Android Full support 51 | Chrome Android Full support 51 | Firefox Android Full support 48 | Opera Android Full support 41 | Safari iOS Full support 10 | Samsung Internet Android Full support 5.0 | nodejs
Full support
6.5.0
|
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.
Note sulla compatibilità
Con ECMAScript 2015, ArrayBuffer deve essere inizializzato con l'operatore new. Inizializzare ArrayBuffer senza l'operatore new genererà un TypeError.
var dv = ArrayBuffer(10); // TypeError: inizializzaree un ArrayBuffer senza il costruttore new // genererà un errore
var dv = new ArrayBuffer(10);