TypedArray
描述了ArrayBuffer
构造
new TypedArray(); // new in ES2017
new TypedArray(length);
new TypedArray(typedArray);
new TypedArray(object);
new TypedArray(buffer [, byteOffset [, length]]);
// where TypedArray is one of:
Int8Array();
Uint8Array();
Uint8ClampedArray();
Int16Array();
Uint16Array();
Int32Array();
Uint32Array();
Float32Array();
Float64Array();
BigInt64Array();
BigUint64Array();
Type | Value Range | Size in bytes | Description | Web IDL type | Equivalent C type |
---|---|---|---|---|---|
Int8Array | -128 to 127 | 1 | 8-bit two’s complement signed integer | byte | int8_t |
Uint8Array | 0 to 255 | 1 | 8-bit unsigned integer | octet | uint8_t |
Uint8ClampedArray | 0 to 255 | 1 | 8-bit unsigned integer (clamped) | octet | uint8_t |
Int16Array | -32768 to 32767 | 2 | 16-bit two’s complement signed integer | short | int16_t |
Uint16Array | 0 to 65535 | 2 | 16-bit unsigned integer | unsigned short | uint16_t |
Int32Array | -2147483648 to 2147483647 | 4 | 32-bit two’s complement signed integer | long i | nt32_t |
Uint32Array | 0 to 4294967295 | 4 | 32-bit unsigned integer | unsigned long | uint32_t |
Float32Array | 1.2×10-38 to 3.4×1038 | 4 | 32-bit IEEE floating point number (7 significant digits e.g., 1.1234567) | unrestricted float | float |
Float64Array | 5.0×10-324 to 1.8×10308 | 8 | 64-bit IEEE floating point number (16 significant digits e.g., 1.123…15) | unrestricted double | double |
BigInt64Array | -263 to 263-1 | 8 | 64-bit two’s complement signed integer | bigint | int64_t (signed long long) |
BigUint64Array | 0 to 264-1 | 8 | 64-bit unsigned integer | bigint | uint64_t (unsigned long long) |
1.int8Array,int16Array都是通过对ArrayBuffer
来进行读写的
2.int8Array是将ArrayBuffer
中的一个字节当成一个元素,Int16Array
则是将ArrayBuffer
中的两个字节当成一个元素.
例如:
1)ArrayBuffer
中有8个字节,分别为01 02 03 04 05 06 07 08
2)分别根据这个ArrayBuffer
创建Uint8,Uint16,和Unit32
var u8 = new Uint8Array(buffer); // length为8
var u16 = new Uint16Array(buffer); // length为4
var u32 = new Uint32Array(buffer); // length为2
3)结果为
[1, 2, 3, 4, 5, 6, 7, 8] //u8
[513, 1027, 1541, 2055] //u16
[67305985, 134678021] //u32
https://www.jb51.net/article/147112.htm
https://developer.51cto.com/art/201704/536718.htm
参考:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Syntax