Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 943915 of ArrayBuffer

  • Revision slug: Web/JavaScript/Reference/Global_Objects/ArrayBuffer
  • Revision title: ArrayBuffer
  • Revision id: 943915
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment review for bug 1214936

Revision Content

{{JSRef}}

The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. You can not directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a {{jsxref("DataView")}} object which represents the buffer in a specific format, and use that to read and write the contents of the 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.

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.
{{jsxref("ArrayBuffer.prototype")}}
Allows the addition of properties to all ArrayBuffer objects.

Methods

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

ArrayBuffer instances

All ArrayBuffer instances inherit from {{jsxref("ArrayBuffer.prototype")}}.

Properties

{{page('en-US/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/prototype','Properties')}}

Methods

{{page('en-US/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/prototype','Methods')}}

Example

In this example, we create a 8-byte buffer with a {{jsxref("Global_Objects/Int32Array", "Int32Array")}} view referring to the buffer:

var buffer = new ArrayBuffer(8);
var view   = new Int32Array(buffer);

Specifications

Specification Status Comment
{{SpecName('Typed Array')}} {{Spec2('Typed Array')}} Superseded by ECMAScript 6.
{{SpecName('ES6', '#sec-arraybuffer-constructor', 'ArrayBuffer')}} {{Spec2('ES6')}} Initial definition in an ECMA standard. Specified that new is required.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 7.0 {{CompatGeckoDesktop("2")}} 10 11.6 5.1
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 4.0 {{CompatVersionUnknown}} {{CompatGeckoMobile("2")}} 10 11.6 4.2

Firefox-specific notes

Starting with Gecko / SpiderMonkey 44 {{geckoRelease(44)}}, ArrayBuffer constructors require to be constructed with a {{jsxref("Operators/new", "new")}} operator. Calling an ArrayBuffer constructor as a function without new, will throw a {{jsxref("TypeError")}} from now on.

var dv = ArrayBuffer(10);
// TypeError: calling a builtin ArrayBuffer constructor 
// without new is forbidden
var dv = new ArrayBuffer(10);

See also

Revision Source

<div>{{JSRef}}</div>

<p>The <strong><code>ArrayBuffer</code></strong> object is used to represent a generic, fixed-length raw binary data buffer. You can not directly manipulate the contents of an <code>ArrayBuffer</code>; instead, you create one of the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray">typed array objects</a> or a {{jsxref("DataView")}} object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
new ArrayBuffer(length)
</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>length</code></dt>
 <dd>The size, in bytes, of the array buffer to create.</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p>A new <code>ArrayBuffer</code> object of the specified size. Its contents are initialized to 0.</p>

<h2 id="Description">Description</h2>

<p>The <code>ArrayBuffer</code> constructor creates a new <code>ArrayBuffer</code> of the given length in bytes.</p>

<h3 id="Getting_an_array_buffer_from_existing_data">Getting an array buffer from existing data</h3>

<ul>
 <li><a href="/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Appendix.3A_Decode_a_Base64_string_to_Uint8Array_or_ArrayBuffer">From a Base64 string</a></li>
 <li><a href="/en-US/docs/Web/API/FileReader#readAsArrayBuffer()">From a local file</a></li>
</ul>

<h2 id="Properties">Properties</h2>

<dl>
 <dt><code>ArrayBuffer.length</code></dt>
 <dd>The <code>ArrayBuffer</code> constructor's length property whose value is 1.</dd>
 <dt>{{jsxref("ArrayBuffer.prototype")}}</dt>
 <dd>Allows the addition of properties to all <code>ArrayBuffer</code> objects.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<dl>
 <dt>{{jsxref("ArrayBuffer.isView", "ArrayBuffer.isView(arg)")}}</dt>
 <dd>Returns <code>true</code> if <code>arg</code> is one of the ArrayBuffer views, such as <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray">typed array objects</a> or a {{jsxref("DataView")}}. Returns <code>false</code> otherwise.</dd>
 <dt>{{jsxref("ArrayBuffer.transfer", "ArrayBuffer.transfer(oldBuffer [, newByteLength])")}} {{experimental_inline}}</dt>
 <dd>
 <div class="line" id="file-arraybuffer-transfer-LC6">Returns a new <code>ArrayBuffer</code> whose contents are taken from the <code>oldBuffer</code>'s data and then is either truncated or zero-extended by <code>newByteLength</code>.</div>
 </dd>
</dl>

<h2 id="ArrayBuffer_instances"><code>ArrayBuffer</code> instances</h2>

<p>All <code>ArrayBuffer</code> instances inherit from {{jsxref("ArrayBuffer.prototype")}}.</p>

<h3 id="Properties_2">Properties</h3>

<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/prototype','Properties')}}</p>

<h3 id="Methods_2">Methods</h3>

<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/prototype','Methods')}}</p>

<h2 id="Example">Example</h2>

<p>In this example, we create a 8-byte buffer with a {{jsxref("Global_Objects/Int32Array", "Int32Array")}} view referring to the buffer:</p>

<pre class="brush: js">
var buffer = new ArrayBuffer(8);
var view   = new Int32Array(buffer);</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Typed Array')}}</td>
   <td>{{Spec2('Typed Array')}}</td>
   <td>Superseded by ECMAScript 6.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-arraybuffer-constructor', 'ArrayBuffer')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition in an ECMA standard. Specified that <code>new</code> is required.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>7.0</td>
   <td>{{CompatGeckoDesktop("2")}}</td>
   <td>10</td>
   <td>11.6</td>
   <td>5.1</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>4.0</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("2")}}</td>
   <td>10</td>
   <td>11.6</td>
   <td>4.2</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Firefox-specific_notes">Firefox-specific notes</h2>

<p>Starting with Gecko / SpiderMonkey 44 {{geckoRelease(44)}}, <code>ArrayBuffer</code> constructors require to be constructed with a {{jsxref("Operators/new", "new")}} operator. Calling an <code>ArrayBuffer</code> constructor as a function without <code>new</code>, will throw a {{jsxref("TypeError")}} from now on.</p>

<pre class="brush: js example-bad">
var dv = ArrayBuffer(10);
// TypeError: calling a builtin ArrayBuffer constructor 
// without new is forbidden</pre>

<pre class="brush: js example-good">
var dv = new ArrayBuffer(10);</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Typed_arrays">JavaScript typed arrays</a></li>
</ul>
Revert to this revision