TypedArr (TC05)
1: const uint8Array = new Uint8Array([0, 128, 255]);
2: const int16Array = new Int16Array(uint8Array); // Create a new view
3:
4:
5: console.log(uint8Array) // Output: Uint8Array(3) [ 0, 128, 255 ]
6: console.log(int16Array); // Output: Int16Array(3) [ 0, 128, 255 ], values are reinterpreted.
7:
8: const float32Array = new Float32Array(uint8Array);
9: console.log(float32Array); // Output: Float32Array(3) [ 0, 128, 255 ]
10:
11: const arrBuf = new ArrayBuffer(12);
12: const x = new Uint8Array(arrBuf);
13: x[0] = 100;
14: x[1] = 101;
15: x[2] = 102;
16: console.log(x) // Output: Uint8Array(12) [ 100, 101, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
17:
18: const y = new Int16Array(arrBuf);
19: console.log(y) // Output: Int16Array(6) [ 25700, 0, 0, 0, 0, 0 ]
20:
21: const z = new Int32Array(arrBuf);
22: console.log(z) //Output: Int32Array(3) [ 673059850, 0, 0 ]
Converting Between Typed Arrays:
Uint8Array(3) [ 0, 128, 255 ]
Int16Array(3) [ 0, 128, 255 ]
Float32Array(3) [ 0, 128, 255 ]
Uint8Array(12) [
100, 101, 102, 0, 0,
0, 0, 0, 0, 0,
0, 0
]
Int16Array(6) [ 25956, 102, 0, 0, 0, 0 ]
Int32Array(3) [ 6710628, 0, 0 ]
TypedArr context:
ES6 context:
Binary context:
Comments (
)
)
Link to this page:
http://www.vb-net.com/JavascriptES6/TC05.htm
|
|