TypedArrays是一组类型,提供对固定长度可变二进制ArrayBuffers的不同视图。在大多数情况下,它们的作用类似于将所有分配的值强制转换为给定数字类型的数组。您可以将ArrayBuffer实例传递给TypedArray构造函数以创建其数据的新视图。
var buffer = new ArrayBuffer(8); var byteView = new Uint8Array(buffer); var floatView = new Float64Array(buffer); console.log(byteView); // [0,0,0,0,0,0,0,0] console.log(floatView); // [0] byteView[0] = 0x01; byteView[1] = 0x02; byteView[2] = 0x04; byteView[3] = 0x08; console.log(floatView); // [6.64421383e-316]
可以使用该.slice(...)方法直接或通过TypedArray视图复制ArrayBuffer 。
var byteView2 = byteView.slice(); var floatView2 = new Float64Array(byteView2.buffer); byteView2[6] = 0xFF; console.log(floatView); // [6.64421383e-316] console.log(floatView2); // [7.06327456e-304]