Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. JavaScript
  3. JavaScript 参考
  4. JavaScript 标准内置对象
  5. Uint16Array

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

Uint16Array

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

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

构造函数

Uint16Array()

创建一个新的Uint16Array 对象。

静态属性

Uint16Array.BYTES_PER_ELEMENT

返回元素大小的数值。在Uint16Array 情况下为2

静态方法

Uint16Array.from()

从类数组或可迭代对象创建一个新的Uint16Array。可参阅Array.from()

Uint16Array.of()

创建一个新的具有可变参数数目的Uint16Array。可参阅Array.of()

实例属性

还从其父接口TypedArray 继承实例属性。

Uint16Array.prototype.buffer

返回Uint16Array 在构造时固定引用的ArrayBuffer。因此是只读的

Uint16Array.prototype.byteLength

返回Uint16ArrayArrayBuffer 开始的长度(以字节为单位)。在构建时固定,因此是只读的

Uint16Array.prototype.byteOffset

返回Uint16ArrayArrayBuffer 开始的偏移量(以字节为单位)。在构建时固定,因此是只读的

Uint16Array.prototype.length

返回Uint16Array 中包含的元素数量。在构建时固定,因此是只读的

实例方法

从其父接口TypedArray 继承实例方法。

示例

创建 Uint16Array 的不同方法

js
// 长度var uint16 = new Uint16Array(2);uint16[0] = 42;console.log(uint16[0]); // 42console.log(uint16.length); // 2console.log(uint16.BYTES_PER_ELEMENT); // 2// 数组var arr = new Uint16Array([21, 31]);console.log(arr[1]); // 31// 另一个类型数组var x = new Uint16Array([21, 31]);var y = new Uint16Array(x);console.log(y[0]); // 21// 一个 ArrayBuffervar buffer = new ArrayBuffer(8);var z = new Uint16Array(buffer, 0, 4);// 可迭代var iterable = (function* () {  yield* [1, 2, 3];})();var uint16 = new Uint16Array(iterable);// Uint16Array[1, 2, 3]

规范

Specification
ECMAScript® 2026 Language Specification
# sec-typedarray-objects

浏览器兼容性

参见

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp