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.
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.
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
ifarg
is one of the ArrayBuffer views, such as typed array objects or aDataView
. Returnsfalse
otherwise. ArrayBuffer.transfer(oldBuffer [, newByteLength])
-
Returns a new
ArrayBuffer
whose contents are taken from theoldBuffer
's data and then is either truncated or zero-extended bynewByteLength
.
Instances
All ArrayBuffer
instances inherit from ArrayBuffer.prototype
.
Properties
Methods
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 (ECMA-262) The definition of 'ArrayBuffer' in that specification. |
Padrão em tempo real |
Compatibilidade entre Navegadores
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
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);