ArrayBuffer é um objeto utilizado para representar um buffer de dados em binário de tamanho pré-definido. Não é possivel manipular o conteudo do buffer diretamente; em vez disso, tem de se criar um dos typed array objects ou um objeto DataView que representa um buffer num formato especifico, e usa-o para ler e escrever o conteudo do buffer. Sintaxe
new ArrayBuffer(length)
Parametros
length- O tamanho, em bytes, do array buffer que se pretende criar.
Retorno
Um novo objecto do tipo ArrayBuffer do tamanho especificado. O respetivo conteudo é inicializado a 0.
Excepções
A RangeError é lançada caso o tamanho (length) é maior do que Number.MAX_SAFE_INTEGER (>= 2 ** 53) ou caso seja negativo.
Descrição
O construtor de ArrayBuffer cria um novo objeto do tipo ArrayBuffer com o tamanho especificado em bytes.
Obter um array buffer a partir de dados existentes
Propriedades
ArrayBuffer.length- Propriedade length do construtor de
ArrayBuffercujo valor é 1. get ArrayBuffer[@@species]- A função do contrutor que é usado para criar objetos derivados.
ArrayBuffer.prototype- Permite adicionar novas propriedades a todos os objetos do tipo
ArrayBuffer.
Métodos
ArrayBuffer.isView(arg)- Devolve
truecasoargé um tipo de representação do ArrayBuffer, como typed array objects ouDataView. Devolvefalsecaso contrário ArrayBuffer.transfer(oldBuffer [, newByteLength])- Devolve um novo objeto
ArrayBuffercujo conteúdo é obtido dooldBuffere ou é truncado ou preenchido a zeros pelonewByteLength.
Instâncias ArrayBuffer
Todas as instâncias de ArrayBuffer herdam de ArrayBuffer.prototype.
Propriedades
- 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.
Métodos
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.
ArrayBuffer.slice()- Tem a mesma funcionalidade que
ArrayBuffer.prototype.slice().
Exemplo
Neste exemplo, criamos um buffer de 8 bytes com representação Int32Array a referênciar o buffer:
var buffer = new ArrayBuffer(8); var view = new Int32Array(buffer);
Especificação
| Especificação | Status | Comentário |
|---|---|---|
| Typed Array Specification | Obsolete | Substituido por ECMAScript 6. |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'ArrayBuffer' in that specification. |
Standard |
Definição inicial no stardard da ECMA. Especificar que |
| ECMAScript Latest Draft (ECMA-262) The definition of 'ArrayBuffer' in that specification. |
Draft |
Compatibilidade nos navegadores
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
| 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) | ? | ? | ? |
ArrayBuffer.slice() |
No support | (Yes) No support 53 (53) |
No support | No support | ? |
| 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) | ? | ? | ? |
ArrayBuffer.slice() |
No support | No support | (Yes) No support 53.0 (53) |
No support | No support | ? |
Notas de compatibilidade
Com ECMAScript 2015, construtores de ArrayBuffer são obrigados a usar o operador new. Daqui adiante ao invocar ArrayBuffer como uma função sem new irá lançar uma exceção TypeError.
var dv = ArrayBuffer(10); // TypeError: calling a builtin ArrayBuffer constructor // without new is forbidden
var dv = new ArrayBuffer(10);