Uint8Array 数组类型表示一个8位无符号整型数组,创建时内容被初始化为0。创建完后,可以以对象的方式或使用数组下标索引的方式引用数组中的元素。
new Uint8Array(); // ES2017 最新语法
new Uint8Array(length); // 创建初始化为0的,包含length个元素的无符号整型数组
new Uint8Array(typedArray);
new Uint8Array(object);
new Uint8Array(buffer [, byteOffset [, length]]);
构造语法和参数的更多信息请参见 TypedArray.
Uint8Array.BYTES_PER_ELEMENTUint8Array.prototype.length.Uint8Array.nameUint8Array.prototypeUint8Array.from()Uint8Array数组,可参见Array.from().Uint8Array.of()Uint8Array数组,可参见Array.of().Uint8Array 原型声明所有的Uint8Array对象继承自 %TypedArray%.prototype.
Uint8Array.prototype.constructorUint8Array 构造器。Uint8Array.prototype.buffer 只读 Uint8Array引用的 ArrayBuffer ,在构造时期固定,所以是只读的。Uint8Array.prototype.byteLength 只读 Uint8Array长度(字节数)。在构造时期固定,所以是 只读的。Uint8Array.prototype.byteOffset 只读 Uint8Array 距离其 ArrayBuffer 起始位置的偏移(字节数)。在构造时期固定,所以是 只读的。Uint8Array.prototype.length 只读 Uint8Array中的元素数量。 在构造时期固定,所以是 只读的。Uint8Array.prototype.copyWithin()Array.prototype.copyWithin()。Uint8Array.prototype.entries()Array Iterator 对象,含有数组中每个下标处的键值对。请参见Array.prototype.entries().Uint8Array.prototype.every()Array.prototype.every()。Uint8Array.prototype.fill()Array.prototype.fill()。Uint8Array.prototype.filter()Array.prototype.filter().Uint8Array.prototype.find()undefined。请参见 Array.prototype.find()。Uint8Array.prototype.findIndex()Array.prototype.findIndex().Uint8Array.prototype.forEach()Array.prototype.forEach()。Uint8Array.prototype.includes() true,否则返回false。另见Array.prototype.includes()。Uint8Array.prototype.indexOf()Array.prototype.indexOf().Uint8Array.prototype.join()Array.prototype.join()。Uint8Array.prototype.keys()Array Iterator ,含有数组中每个下标的键,请参见 Array.prototype.keys()。Uint8Array.prototype.lastIndexOf()Array.prototype.lastIndexOf()。Uint8Array.prototype.map()Array.prototype.map()。Uint8Array.prototype.move() 未实现Uint8Array.prototype.copyWithin()的之前的非标准版本。Uint8Array.prototype.reduce()Array.prototype.reduce()。Uint8Array.prototype.reduceRight()Array.prototype.reduceRight()。Uint8Array.prototype.reverse()Array.prototype.reverse()。Uint8Array.prototype.set()Uint8Array.prototype.slice()Array.prototype.slice()。Uint8Array.prototype.some()true。请参见Array.prototype.some()。Uint8Array.prototype.sort()Array.prototype.sort()。Uint8Array.prototype.subarray()从给定的元素起始和终止下标返回新的 Uint8Array 。Uint8Array.prototype.values()Array Iterator 对象,含有数组每个下标处的值,请参见 Array.prototype.values()。Uint8Array.prototype.toLocaleString()Array.prototype.toLocaleString()。Uint8Array.prototype.toString()Array.prototype.toString()。Uint8Array.prototype[@@iterator]()返回新的 Array Iterator 对象,包含数组中每个下标处的值。// 来自长度
var uint8 = new Uint8Array(2);
uint8[0] = 42;
console.log(uint8[0]); // 42
console.log(uint8.length); // 2
console.log(uint8.BYTES_PER_ELEMENT); // 1
// 来自数组
var arr = new Uint8Array([21,31]);
console.log(arr[1]); // 31
// 来自另一个 TypedArray
var x = new Uint8Array([21, 31]);
var y = new Uint8Array(x);
console.log(y[0]); // 21
// 来自 ArrayBuffer
var buffer = new ArrayBuffer(8);
var z = new Uint8Array(buffer, 1, 4);
// 来自一个迭代器
var iterable = function*(){ yield* [1,2,3]; }();
var uint8 = new Uint8Array(iterable);
// Uint8Array[1, 2, 3]
| Specification | Status | Comment |
|---|---|---|
| 类型化数组规范 | Obsolete | 由 ECMAScript 6 取代。 |
| ECMAScript 2015 (6th Edition, ECMA-262) TypedArray constructors |
Standard | ECMA 标准中的初始定义。 |
| Desktop | Mobile | Server | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Uint8Array | Chrome Full support 7 | Edge Full support Yes | Firefox Full support 4 | IE Full support 10 | Opera Full support 11.6 | Safari Full support 5.1 | WebView Android Full support 4 | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support 11.6 | Safari iOS Full support 4.2 | Samsung Internet Android Full support Yes | nodejs Full support 0.10 |
Uint8Array() without new throws | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 44 | IE No support No | Opera Full support Yes | Safari ? | WebView Android ? | Chrome Android ? | Edge Mobile ? | Firefox Android Full support 44 | Opera Android ? | Safari iOS ? | Samsung Internet Android ? | nodejs Full support 0.12 |
| Iterable in constructor | Chrome ? | Edge ? | Firefox Full support 52 | IE ? | Opera ? | Safari ? | WebView Android ? | Chrome Android ? | Edge Mobile ? | Firefox Android Full support 52 | Opera Android ? | Safari iOS ? | Samsung Internet Android ? | nodejs Full support 4.0.0 |
| Constructor without arguments | Chrome ? | Edge ? | Firefox Full support 55 | IE ? | Opera ? | Safari ? | WebView Android ? | Chrome Android ? | Edge Mobile ? | Firefox Android Full support 55 | Opera Android ? | Safari iOS ? | Samsung Internet Android ? | nodejs ? |
从 ECMAScript 2015 开始,Uint8Array 构造函数需要通过 new 操作符调用。即日起如果没有使用 new 调用 Uint8Array 的构造函数,将会抛出 TypeError 。
var dv = Uint8Array([1, 2, 3]);
// TypeError: calling a builtin Uint8Array constructor
// 不使用 new 将会被禁止
var dv = new Uint8Array([1, 2, 3]);