JavaScript | DataView()
Last Updated :
25 Jun, 2018
The DataView function in JavaScript provides an interface to read and write more than one number types into an ArrayBuffer.
Syntax:
new DataView(buffer, byteOffset, byteLength)
Parameters: The function accepts three parameters which are described as below:
- buffer: An ArrayBuffer that is already existing to store the new DataView object.
- byteOffset (optional): offset(in bytes) in the buffer is used to start a new view of the buffer. By default, the new view starts from the first byte.
- byteLength (optional): It represents number of elements in the byte array. By default, the buffer’s length is considered as the length of the view.
Return value: It returns a new DataView object which will represent the specified data buffer.
JavaScript code to show the working of DataView() function:
Code#1:
<script>
var buffer = new ArrayBuffer(16);
var view1 = new DataView(buffer);
var view2 = new DataView(buffer,0,4);
var view3 = new DataView(buffer,12,2);
view1.setInt8(0, 1);
view1.setInt8(12, 2)
document.write(view2.getInt8(0)+ '<br>' );
document.write(view3.getInt8(0)+ '<br>' );
</script>
|
Output:
1
2
Share your thoughts in the comments
Please Login to comment...