ArrayBuffer

Esta tradução está incompleta. Ajude a traduzir este artigo em inglês

O objeto ArrayBuffer é um tipo de dado usado para representar um genérico, buffer de dados binários de tamanho fixo. Você não pode manipular diretamente os conteúdos de um ArrayBuffer;  em vez disso, você cria um objeto ArrayBufferView que representa o buffer em um formato específico, e usa para ler e escrever os conteúdos do buffer.

Syntax

new ArrayBuffer(length)

Parameters

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

Return value

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

Exceptions

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

Description

The ArrayBuffer constructor creates a new ArrayBuffer of the given length in bytes.

Getting an array buffer from existing data

Properties

ArrayBuffer.length
The ArrayBuffer constructor'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 ArrayBuffer objects.

Methods

ArrayBuffer.isView(arg)
Returns true if arg is one of the ArrayBuffer views, such as typed array objects or a DataView. Returns false otherwise.
ArrayBuffer.transfer(oldBuffer [, newByteLength])

Returns a new ArrayBuffer whose contents are taken from the oldBuffer's data and then is either truncated or zero-extended by newByteLength.

Instances

All ArrayBuffer instances inherit from ArrayBuffer.prototype.

Properties

ArrayBuffer.length
The ArrayBuffer constructor'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 ArrayBuffer objects.

Methods

ArrayBuffer.isView(arg)
Returns true if arg is one of the ArrayBuffer views, such as typed array objects or a DataView. Returns false otherwise.
ArrayBuffer.transfer(oldBuffer [, newByteLength])
Returns a new ArrayBuffer whose contents are taken from the oldBuffer's data and then is either truncated or zero-extended by newByteLength.
ArrayBuffer.slice()
Has the same functionality as ArrayBuffer.prototype.slice().

Exemplo

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);

Especificações

Especificação Status Comentário
Typed Array Specification Obsoleto Substituído pelo ECMAScript 6
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'ArrayBuffer' in that specification.
Padrão Definição inicial no ECMA standard. Specified that new is required.
ECMAScript Latest Draft (ECMA-262)
The definition of 'ArrayBuffer' in that specification.
Rascunho  

Compatibilidade entre Navegadores

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
ArrayBufferChrome Full support 7Edge Full support 12Firefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
ArrayBuffer() constructorChrome Full support 7Edge Full support 12Firefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
byteLengthChrome Full support 7Edge Full support 12Firefox Full support 4IE Full support 10Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 4.2Samsung Internet Android Full support 1.0nodejs Full support Yes
isViewChrome Full support 32Edge Full support 12Firefox Full support 29IE Full support 11Opera Full support 19Safari Full support 7WebView Android Full support ≤37Chrome Android Full support 32Firefox Android Full support 29Opera Android Full support 19Safari iOS Full support 7Samsung Internet Android Full support 2.0nodejs Full support Yes
sliceChrome Full support 17Edge Full support 12Firefox Full support 12
Notes
Full support 12
Notes
Notes The non-standard ArrayBuffer.slice() method has been removed in Firefox 53 (but the standardized version ArrayBuffer.prototype.slice() is kept.
IE Full support 11Opera Full support 12.1Safari Full support 6WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 14
Notes
Full support 14
Notes
Notes The non-standard ArrayBuffer.slice() method has been removed in Firefox 53 (but the standardized version ArrayBuffer.prototype.slice() is kept.
Opera Android Full support 12.1Safari iOS Full support 6Samsung Internet Android Full support 1.0nodejs Full support Yes
@@speciesChrome Full support 51Edge Full support 13Firefox Full support 48IE No support NoOpera Full support 38Safari Full support 10WebView Android Full support 51Chrome Android Full support 51Firefox Android Full support 48Opera Android Full support 41Safari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 6.5.0
Full support 6.5.0
Full support 6.0.0
Disabled
Disabled From version 6.0.0: this feature is behind the --harmony runtime flag.

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.

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); 
// TypedError: calling a builtin ArrayBuffer constructor
// without new is forbidden
var dv = new ArrayBuffer(10);

Veja também