W3cubDocs

/JavaScript

DataView.dataView

The DataView() constructor is used to create DataView objects.

Syntax

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

Parameters

buffer
An existing ArrayBuffer or SharedArrayBuffer to use as the storage backing the new DataView object.
byteOffset Optional
The offset, in bytes, to the first byte in the above buffer for the new view to reference. If unspecified, the buffer view starts with the first byte.
byteLength Optional
The number of elements in the byte array. If unspecified, the view's length will match the buffer's length.

Return value

A new DataView object representing the specified data buffer. (That probably wasn't a very helpful description.)

You can think of the returned object as an "interpreter" of the array buffer of bytes — it knows how to convert numbers to fit within the buffer correctly, both when reading and writing to it. This means handling integer and float conversion, endianness, and other details of representing numbers in binary form.

Exceptions

RangeError

Thrown if the byteOffset or byteLength parameter values result in the view extending past the end of the buffer.

For example, if the buffer is 16 bytes long, the byteOffset is 8, and the byteLength is 10, this error is thrown because the resulting view tries to extend 2 bytes past the total length of the buffer.

Examples

Using DataView

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

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

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
DataView() constructor 9 12 15 10 12.1 5.1
DataView() without new throws 11 13 40 No 15 5.1
SharedArrayBuffer accepted as buffer 68
68
60 — 63
Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This was a temporary removal while mitigations were put in place.
79 79
79
57
Disabled
Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog).
Disabled From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.
55 — 57
46 — 55
Disabled
Disabled From version 46 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No No 10.1 — 11
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
DataView() constructor 4 18 15 12.1 4.2 1.0
DataView() without new throws ≤37 18 40 14 5.1 1.0
SharedArrayBuffer accepted as buffer 60 — 63
60 — 63
Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.
60 — 63
60 — 63
Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.
79
79
57
Disabled
Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog).
Disabled From version 57: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.
55 — 57
46 — 55
Disabled
Disabled From version 46 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No 10.3 — 11 No
No
Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.
Server
Node.js
DataView() constructor 0.10
DataView() without new throws 0.10
SharedArrayBuffer accepted as buffer 8.10.0

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/DataView