此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
Uint32Array
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月.
Uint32Array 表示一个由基于平台字节序的 32 位无符号字节组成的数组。如果需要对字节顺序进行控制 (译者注:即 littleEndian 或 bigEndian),请使用DataView 代替。数组中每个元素的初始值都是0。一旦创建,你可以用对象的方法引用数组里的元素,或者使用标准的数组索引语法(即,使用中括号)。
In this article
语法
new Uint32Array(); // new in ES2017new Uint32Array(length);new Uint32Array(typedArray);new Uint32Array(object);new Uint32Array(buffer [, byteOffset [, length]]);
更多的构造器语法和属性请参照TypedArray。
静态属性
Uint32Array.BYTES_PER_ELEMENT返回一个数值,代表
Uint32Array中单个元素的字节大小。Uint32Array返回4。- Uint32Array.length
固定值 (static) 属性,值为 3。使用
Uint32Array.prototype.length获得数组的真实长度(元素个数)。Uint32Array.prototypeTypedArray 对象的原型链。
静态方法
Uint32Array.from()从类似数组或者可迭代对象创建一个新的
Uint32Array。请参考Array.from()。Uint32Array.of()从可变长度的参数创建一个新的
Uint32Array。请参考Array.of()。
实例属性
还从其父接口TypedArray 继承实例属性。
Uint32Array.prototype.constructor返回创建实例原型的函数。默认返回
Uint32Array的构造器。Uint32Array.prototype.buffer只读返回
Uint32Array引用的ArrayBuffer。由于构造时已固定,所以是只读的。Uint32Array.prototype.byteLength只读返回从其
ArrayBuffer开始的Uint32Array字节长度。由于构造时已固定,所以是只读的。Uint32Array.prototype.byteOffset只读返回从其
ArrayBuffer的偏移开始的Uint32Array字节长度。由于构造时已固定,所以是只读的。Uint32Array.prototype.length只读返回
Uint32Array中元素的个数。由于构造时已固定,所以是只读的。
实例方法
从其父接口TypedArray 继承实例方法。
示例
用不同的方法创建Uint32Array:
// 给定长度var uint32 = new Uint32Array(2);uint32[0] = 42;console.log(uint32[0]); // 42console.log(uint32.length); // 2console.log(uint32.BYTES_PER_ELEMENT); // 4// 给定数组var arr = new Uint32Array([21, 31]);console.log(arr[1]); // 31// 给定 TypedArrayvar x = new Uint32Array([21, 31]);var y = new Uint32Array(x);console.log(y[0]); // 21// 给定 ArrayBuffervar buffer = new ArrayBuffer(16);var z = new Uint32Array(buffer, 0, 4);// 给定可迭代对象var iterable = (function* () { yield* [1, 2, 3];})();var uint32 = new Uint32Array(iterable);// Uint32Array[1, 2, 3]规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-typedarray-objects> |