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.name返回构造函数名称的字符串值。在Uint16Array类型的情况下:“Uint16Array”。TypedArray对象的Uint16Array.prototype原型。
方法
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.byteLength只读返回Uint16Array从其开始的长度(以字节为单位)ArrayBuffer。在构造时被固定,因此只读。
Uint16Array.prototype.byteOffset只读返回Uint16Array从其开始的偏移量(以字节为单位)ArrayBuffer。在构造时被固定,因此只读。
Uint16Array.prototype.length只读返回保存在元素中的元素的数量Uint16Array。在构造时被固定,因此只读。
方法
Uint16Array.prototype.copyWithin()复制数组中的一系列数组元素。另见Array.prototype.copyWithin()。
Uint16Array.prototype.entries()返回Array Iterator包含数组中每个索引的键/值对的新对象。另见Array.prototype.entries()。
Uint16Array.prototype.every()测试数组中的所有元素是否都通过了函数提供的测试。另见Array.prototype.every()。
Uint16Array.prototype.fill()用静态值填充从开始索引到结束索引的数组的所有元素。另见Array.prototype.fill()。
Uint16Array.prototype.filter()使用提供的过滤函数返回true的数组的所有元素创建一个新数组。另见Array.prototype.filter()。
Uint16Array.prototype.find()如果数组中的元素满足提供的测试函数或者undefined未找到,则返回数组中找到的值。另见Array.prototype.find()。
Uint16Array.prototype.findIndex()如果数组中的元素满足提供的测试函数,则返回数组中找到的索引;如果未找到,则返回-1。另见Array.prototype.findIndex()。
Uint16Array.prototype.forEach()为数组中的每个元素调用一个函数。另见Array.prototype.forEach()。
Uint16Array.prototype.includes()确定类型数组是否包含某个元素,返回true还是false适当的。另见Array.prototype.includes()。
Uint16Array.prototype.indexOf()返回数组中等于指定值的元素的第一个(最少)索引,如果没有找到,则返回-1。另见Array.prototype.indexOf()。
Uint16Array.prototype.join()将数组的所有元素连接成一个字符串。另见Array.prototype.join()。
Uint16Array.prototype.keys()返回Array Iterator包含数组中每个索引的键的新键。另见Array.prototype.keys()。
Uint16Array.prototype.lastIndexOf()返回数组中等于指定值的元素的最后一个(最大)索引,如果没有找到,则返回-1。另见Array.prototype.lastIndexOf()。
Uint16Array.prototype.map()创建一个新的数组,并在该数组中的每个元素上调用一个提供的函数的结果。另见Array.prototype.map()。
Uint16Array.prototype.move()UnimplementedFormer的非标准版本Uint16Array.prototype.copyWithin()。
Uint16Array.prototype.reduce()对累加器和数组的每个值应用一个函数(从左到右)以将其减少到单个值。另见Array.prototype.reduce()。
Uint16Array.prototype.reduceRight()对累加器和数组的每个值(从右到左)应用一个函数,以将其减少为单个值。另见Array.prototype.reduceRight()。
Uint16Array.prototype.reverse()颠倒数组元素的顺序 - 第一个变成最后一个,最后一个变成第一个。另见Array.prototype.reverse()。
Uint16Array.prototype.set()在类型数组中存储多个值,从指定的数组读取输入值。
Uint16Array.prototype.slice()提取数组的一部分并返回一个新的数组。另见Array.prototype.slice()。
Uint16Array.prototype.some()如果此数组中至少有一个元素满足提供的测试函数,则返回true。另见Array.prototype.some()。
Uint16Array.prototype.sort()对数组中的元素进行排序并返回数组。另见Array.prototype.sort()。从给定的开始和结束元素索引中Uint16Array.prototype.subarray()返回一个新Uint16Array的。
Uint16Array.prototype.values()返回Array Iterator包含数组中每个索引值的新对象。另见Array.prototype.values()。
Uint16Array.prototype.toLocaleString()返回表示数组及其元素的本地化字符串。另见Array.prototype.toLocaleString()。
Uint16Array.prototype.toString()返回表示数组及其元素的字符串。另见Array.prototype.toString()。
Uint16Array.prototype[@@iterator]()返回一个新的Array Iterator 包含数组中每个索引值的对象。
示例
不同的方式来创建一个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]规范
Specification | Status | Comment |
|---|---|---|
Typed Array Specification | Obsolete | Superseded by ECMAScript 2015. |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'TypedArray constructors' in that specification. | Standard | Initial definition in an ECMA standard. Specified that new is required. |
ECMAScript Latest Draft (ECMA-262)The definition of 'TypedArray constructors' in that specification. | Draft | ECMAScript 2017 changed the Uint16Array constructor to use the ToIndex operation and allows constructors with no arguments. |
浏览器兼容性
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
Basic support | 7.0 | (Yes) | 4.0 (2) | 10 | 11.6 | 5.1 |
new is required | ? | ? | 44 (44) | ? | ? | ? |
Iterable in constructor | ? | ? | 52 (52) | ? | ? | ? |
Constructor without arguments | ? | ? | 55 (55) | ? | ? | ? |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|---|
Basic support | 4.0 | (Yes) | (Yes) | 4.0 (2) | 10 | 11.6 | 4.2 |
new is required | ? | ? | ? | 44.0 (44) | ? | ? | ? |
Iterable in constructor | ? | ? | ? | 52.0 (52) | ? | ? | ? |
Constructor without arguments | ? | ? | ? | 55.0 (55) | ? | ? | ? |
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

