在线文档教程

Uint16Array

Uint16Array

Uint16Array 类型数组表示在平台字节顺序中的16位无符号整数数组。如果需要控制字节顺序, 请改用 DataView。内容初始化为0。一旦建立, 您可以使用对象的方法引用数组中的元素, 或者使用标准数组索引语法 (即使用括号表示法)。

语法

new Uint16Array( // new in ES2017 new Uint16Array(length new Uint16Array(typedArray new Uint16Array(object new Uint16Array(buffer [, byteOffset [, length]]

有关构造函数语法和参数的更多信息, 请参见 TypedArray。

属性

Uint16Array.BYTES_PER_ELEMENT返回元素大小的数字值。在Uint16Array.Uint16Array.lengthStatic长度属性的情况下,其值为0.对于实际长度(元素数量),请参阅Uint16Array.prototype.length

方法

Uint16Array.from()Uint16Array从类似数组或类的对象中创建一个新的对象。另见Array.from()。用可变数量的参数Uint16Array.of()创建一个新Uint16Array的。另见Array.of()

Uint16Array prototype

所有Uint16Array对象都从中继承%TypedArray%.prototype

Properties

Uint16Array.prototype.constructor返回创建实例原型的函数。这是Uint16Array默认的构造函数。Uint16Array.prototype.buffer在构造时被固定,因此只读,ArrayBuffer引用参考Uint16Array。

方法

Uint16Array.prototype.copyWithin()复制数组中的一系列数组元素。另见Array.prototype.copyWithin()

示例

不同的方式来创建一个Uint16Array

// From a length var uint16 = new Uint16Array(2 uint16[0] = 42; console.log(uint16[0] // 42 console.log(uint16.length // 2 console.log(uint16.BYTES_PER_ELEMENT // 2 // From an array var arr = new Uint16Array([21,31] console.log(arr[1] // 31 // From another TypedArray var x = new Uint16Array([21, 31] var y = new Uint16Array(x console.log(y[0] // 21 // From an ArrayBuffer var buffer = new ArrayBuffer(8 var z = new Uint16Array(buffer, 0, 4 // From an iterable var iterable = function*(){ yield* [1,2,3]; }( var uint16 = new Uint16Array(iterable // Uint16Array[1, 2, 3]

规范

SpecificationStatusComment
Typed Array SpecificationObsoleteSuperseded by ECMAScript 2015.
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'TypedArray constructors' in that specification.StandardInitial definition in an ECMA standard. Specified that new is required.
ECMAScript Latest Draft (ECMA-262)The definition of 'TypedArray constructors' in that specification.DraftECMAScript 2017 changed the Uint16Array constructor to use the ToIndex operation and allows constructors with no arguments.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support7.0(Yes)4.0 (2)1011.65.1
new is required??44 (44)???
Iterable in constructor??52 (52)???
Constructor without arguments??55 (55)???

FeatureAndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support4.0(Yes)(Yes)4.0 (2)1011.64.2
new is required???44.0 (44)???
Iterable in constructor???52.0 (52)???
Constructor without arguments???55.0 (55)???