DataView() constructor
The DataView()
constructor is used to create DataView
objects.
Syntax
new DataView(buffer) new DataView(buffer, byteOffset) new DataView(buffer, byteOffset, byteLength)
Parameters
buffer
-
An existing
ArrayBuffer
orSharedArrayBuffer
to use as the storage backing the newDataView
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
orbyteLength
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 thebyteLength
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 compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
DataView |
9 |
12 |
15 |
10 |
12.1 |
5.1 |
4 |
18 |
15 |
12.1 |
5 |
1.0 |
new_required |
11 |
13 |
40 |
No |
15 |
5.1 |
≤37 |
18 |
40 |
14 |
5 |
1.0 |
sharedarraybuffer_support |
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
57
Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog).
55-57
46-55
|
No |
No |
10.1-11 |
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
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
57
Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog).
55-57
46-55
|
No |
10.3-11 |
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.
|
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/DataView