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 1129007 of DataView

  • Revision slug: Web/JavaScript/Reference/Global_Objects/DataView
  • Revision title: DataView
  • Revision id: 1129007
  • Created:
  • Creator: Taoja
  • Is current revision? No
  • Comment there have no length properties in DataView

Revision Content

{{JSRef}}

The DataView view provides a low-level interface for reading and writing multiple number types in an {{jsxref("ArrayBuffer")}} irrespective of the platform's endianness.

Syntax

new DataView(buffer [, byteOffset [, byteLength]])

Parameters

buffer
An existing {{jsxref("ArrayBuffer")}} to use as the storage for the new DataView object.
byteOffset {{optional_inline}}
The offset, in bytes, to the first byte in the specified buffer for the new view to reference. If not specified, the view of the buffer will start with the first byte.
byteLength {{optional_inline}}
The number of elements in the byte array. If unspecified, length of the view will match the buffer's length.

Return value

A new DataView object representing the specified data buffer.

Errors thrown

{{jsxref("RangeError")}}
Thrown if the byteOffset and byteLength result in the specified view extending past the end of the buffer.

Description

Endianness

Multi-byte number formats are represented in memory differently depending on machine architecture, see {{Glossary("Endianness")}} for an explanation. DataView accessors provide explicit control of how data will be accessed irrespective of the platform architecture's endianness.

var littleEndian = (function() {
  var buffer = new ArrayBuffer(2);
  new DataView(buffer).setInt16(0, 256, true /* littleEndian */);
  // Int16Array uses the platform's endianness.
  return new Int16Array(buffer)[0] === 256;
})();
console.log(littleEndian); // true or false

Properties

{{jsxref("DataView.prototype")}}
Allows the addition of properties to all DataView objects.

DataView instances

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

Properties

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

Methods

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

Example

var buffer = new ArrayBuffer(16);
var dv = new DataView(buffer, 0);

dv.setInt16(1, 42);
dv.getInt16(1); //42

Specifications

Specification Status Comment
{{SpecName('Typed Array')}} {{Spec2('Typed Array')}} Superseded by ECMAScript 6
{{SpecName('ES6', '#sec-dataview-constructor', 'DataView')}} {{Spec2('ES6')}} Initial definition in an ECMA standard
{{SpecName('ESDraft', '#sec-dataview-constructor', 'DataView')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 9.0 {{CompatGeckoDesktop("15.0")}} 10 12.1 5.1
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 4.0 {{CompatVersionUnknown}} {{CompatGeckoMobile("15")}} {{CompatUnknown}} 12.0 4.2

Firefox-specific notes

Starting with Gecko / SpiderMonkey 40 {{geckoRelease(40)}}, DataView requires to be constructed with a {{jsxref("Operators/new", "new")}} operator. Calling DataView() as a function without new, will throw a {{jsxref("TypeError")}} from now on.

var dv = DataView(buffer, 0); 
// TypeError: calling a builtin DataView constructor without new is forbidden
var dv = new DataView(buffer, 0);

See also

  • jDataView: JavaScript library that polyfills and extends the DataView API to all browsers and Node.js.

Revision Source

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

<p>The <strong><code>DataView</code></strong> view provides a low-level interface for reading and writing multiple number types&nbsp;in an {{jsxref("ArrayBuffer")}} irrespective of the platform's endianness.</p>

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

<pre class="syntaxbox">
new DataView(buffer [, byteOffset [, byteLength]])</pre>

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

<dl>
 <dt><code>buffer</code></dt>
 <dd>An existing {{jsxref("ArrayBuffer")}} to use as the storage for the new <code>DataView</code> object.</dd>
 <dt><code>byteOffset</code> {{optional_inline}}</dt>
 <dd>The offset, in bytes, to the first byte in the specified buffer for the new view to reference. If not specified, the view of the buffer will start with the first byte.</dd>
 <dt><code>byteLength</code> {{optional_inline}}</dt>
 <dd>The number of elements in the byte array. If unspecified, length of the view will match the buffer's length.</dd>
</dl>

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

<p>A new <code>DataView</code> object representing the specified data buffer.</p>

<h3 id="Errors_thrown">Errors thrown</h3>

<dl>
 <dt><code>{{jsxref("RangeError")}}</code></dt>
 <dd>Thrown if the <code>byteOffset</code> and <code>byteLength</code> result in the specified view extending past the end of the buffer.</dd>
</dl>

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

<h3 id="Endianness">Endianness</h3>

<p>Multi-byte number formats are represented in memory differently depending on machine&nbsp;architecture, see {{Glossary("Endianness")}} for an explanation. DataView accessors provide explicit control of how data will be accessed irrespective of the platform architecture's endianness.</p>

<pre class="brush: js">
var littleEndian = (function() {
  var buffer = new ArrayBuffer(2);
  new DataView(buffer).setInt16(0, 256, true /* littleEndian */);
  // Int16Array uses the platform's endianness.
  return new Int16Array(buffer)[0] === 256;
})();
console.log(littleEndian); // true or false
</pre>

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

<dl>
 <dt>{{jsxref("DataView.prototype")}}</dt>
 <dd>Allows the addition of properties to all <code>DataView</code> objects.</dd>
</dl>

<h2 id="DataView_instances"><code>DataView</code> instances</h2>

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

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

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

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

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

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

<pre class="brush: js">
var buffer = new ArrayBuffer(16);
var dv = new DataView(buffer, 0);

dv.setInt16(1, 42);
dv.getInt16(1); //42
</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-dataview-constructor', 'DataView')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition in an ECMA standard</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-dataview-constructor', 'DataView')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</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>9.0</td>
   <td>{{CompatGeckoDesktop("15.0")}}</td>
   <td>10</td>
   <td>12.1</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("15")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>12.0</td>
   <td>4.2</td>
  </tr>
 </tbody>
</table>
</div>

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

<p>Starting with Gecko / SpiderMonkey 40 {{geckoRelease(40)}}, <code>DataView</code> requires to be constructed with a {{jsxref("Operators/new", "new")}} operator. Calling <code>DataView()</code> as a function without <code>new</code>, will throw a {{jsxref("TypeError")}} from now on.</p>

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

<pre class="brush: js example-good">
var dv = new DataView(buffer, 0);</pre>

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

<ul>
 <li><a class="link-https" href="https://github.com/jDataView/jDataView">jDataView</a>: JavaScript library that polyfills and extends the <code>DataView</code> API to all browsers and Node.js.</li>
</ul>
Revert to this revision