The DataView()
constructor creates DataView objects.
Syntax
new DataView(buffer)
new DataView(buffer, byteOffset)
new DataView(buffer, byteOffset, byteLength)
Note:
DataView()
can only be constructed withnew
. Attempting to call it withoutnew
throws a TypeError.
Parameters
buffer
- : An existing ArrayBuffer or SharedArrayBuffer to use as
the storage backing the new
DataView
object.
- : An existing ArrayBuffer or SharedArrayBuffer to use as
the storage backing the new
byteOffset
- : 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
- : 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.
Exceptions
- RangeError
- : Thrown if the
byteOffset
orbyteLength
parameter values result in the view extending past the end of the buffer. In other words,byteOffset + byteLength > buffer.byteLength
.
- : Thrown if the
Examples
Using DataView
const buffer = new ArrayBuffer(16);
const view = new DataView(buffer, 0);
view.setInt16(1, 42);
view.getInt16(1); // 42