aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/uint32array/index.html
blob: 1239590bdfa5aa9dc71816aff92ab6efede3efbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
---
title: Uint32Array
slug: Web/JavaScript/Reference/Global_Objects/Uint32Array
translation_of: Web/JavaScript/Reference/Global_Objects/Uint32Array
---
<div>{{JSRef("Global_Objects", "TypedArray", "Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array")}}</div>

<div></div>

<p><strong><code>Uint32Array</code> </strong>表示一个由基于平台字节序的32位无符号字节组成的数组。如果需要对字节顺序进行控制(译者注:即 littleEndian 或 bigEndian),请使用 {{jsxref("DataView")}} 代替。数组中每个元素的初始值都是<code>0</code>。一旦创建,你可以用对象的方法引用数组里的元素,或者使用标准的数组索引语法(即,使用中括号)。</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">new Uint32Array(); // new in ES2017
new Uint32Array(length);
new Uint32Array(typedArray);
new Uint32Array(object);
new Uint32Array(buffer [, byteOffset [, length]]);</pre>

<p>更多的构造器语法和属性请参照 <em><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Syntax">TypedArray</a></em></p>

<h2 id="属性">属性</h2>

<dl>
 <dt>{{jsxref("TypedArray.BYTES_PER_ELEMENT", "Uint32Array.BYTES_PER_ELEMENT")}}</dt>
 <dd>返回一个数值,代表<code>Uint32Array</code>中单个元素的字节大小。<code>Uint32Array</code> 返回 <code>4</code></dd>
 <dt>Uint32Array.length</dt>
 <dd>固定值(static)属性,值为3。使用 {{jsxref("TypedArray.prototype.length", "Uint32Array.prototype.length")}} 获得数组的真实长度(元素个数)。</dd>
 <dt>{{jsxref("TypedArray.name", "Uint32Array.name")}}</dt>
 <dd>返回字符串类型的值,表示构造器的名字。<code>Uint32Array</code> 的返回值是: "Uint32Array"。</dd>
 <dt>{{jsxref("TypedArray.prototype", "Uint32Array.prototype")}}</dt>
 <dd><em>TypedArray</em> 对象的原型链。</dd>
</dl>

<h2 id="方法">方法</h2>

<dl>
 <dt>{{jsxref("TypedArray.from", "Uint32Array.from()")}}</dt>
 <dd>从类似数组或者可迭代对象创建一个新的 <code>Uint32Array</code> 。请参考 {{jsxref("Array.from()")}}.</dd>
 <dt>{{jsxref("TypedArray.of", "Uint32Array.of()")}}</dt>
 <dd>从可变长度的参数创建一个新的 <code>Uint32Array</code> 。请参考 {{jsxref("Array.of()")}}.</dd>
</dl>

<h2 id="Boolean_instances" name="Boolean_instances"><code>Uint32Array</code> 原型</h2>

<p><font face="Open Sans, sans-serif">所有 </font><code>Uint32Array</code> 对象继承自 {{jsxref("TypedArray.prototype", "%TypedArray%.prototype")}}.</p>

<h3 id="属性_2">属性</h3>

<dl>
 <dt><code>Uint32Array.prototype.constructor</code></dt>
 <dd>返回创建实例原型的函数。 默认返回 <code>Uint32Array</code> 的构造器。</dd>
 <dt>{{jsxref("TypedArray.prototype.buffer", "Uint32Array.prototype.buffer")}} {{readonlyInline}}</dt>
 <dd>返回 <code>Uint32Array</code>引用的 {{jsxref("ArrayBuffer")}}。 由于构造时已固定,所以是<strong>只读的</strong></dd>
 <dt>{{jsxref("TypedArray.prototype.byteLength", "Uint32Array.prototype.byteLength")}} {{readonlyInline}}</dt>
 <dd>返回从其 {{jsxref("ArrayBuffer")}} 开始的 <code>Uint32Array</code> 字节长度。由于构造时已固定,所以是<strong>只读的</strong></dd>
 <dt>{{jsxref("TypedArray.prototype.byteOffset", "Uint32Array.prototype.byteOffset")}} {{readonlyInline}}</dt>
 <dd>返回从其 {{jsxref("ArrayBuffer")}} 的偏移开始的 <code>Uint32Array</code> 字节长度。由于构造时已固定,所以是<strong>只读的</strong></dd>
 <dt>{{jsxref("TypedArray.prototype.length", "Uint32Array.prototype.length")}} {{readonlyInline}}</dt>
 <dd>返回 <code>Uint32Array</code>  中元素的个数。由于构造时已固定,所以是<strong>只读的</strong></dd>
</dl>

<h3 id="方法_2">方法</h3>

<dl>
 <dt>{{jsxref("TypedArray.copyWithin", "Uint32Array.prototype.copyWithin()")}}</dt>
 <dd>拷贝(浅拷贝)数组的部分元素到本数组的不同位置(不改变数组的大小)。请参考 {{jsxref("Array.prototype.copyWithin()")}}</dd>
 <dt>{{jsxref("TypedArray.entries", "Uint32Array.prototype.entries()")}}</dt>
 <dd>返回一个 <code>Array Iterator</code> 对象,该对象包含数组中每一对索引的键值对。请参考 {{jsxref("Array.prototype.entries()")}}</dd>
 <dt>{{jsxref("TypedArray.every", "Uint32Array.prototype.every()")}}</dt>
 <dd>测试是否数组中的所有元素都通过给定的测试函数。请参考 {{jsxref("Array.prototype.every()")}}</dd>
 <dt>{{jsxref("TypedArray.fill", "Uint32Array.prototype.fill()")}}</dt>
 <dd>使用静态值填充从起始下标到终止下标的数组元素。请参考 {{jsxref("Array.prototype.fill()")}}</dd>
 <dt>{{jsxref("TypedArray.filter", "Uint32Array.prototype.filter()")}}</dt>
 <dd>创造一个新数组,含有原数组中可通过给定的过滤器函数的所有元素。请参考 {{jsxref("Array.prototype.filter()")}}</dd>
 <dt>{{jsxref("TypedArray.find", "Uint32Array.prototype.find()")}}</dt>
 <dd>如果数组中的元素满足提供的测试函数,返回找到的值,如果没有找到则返回 <code>undefined</code>。请参考 {{jsxref("Array.prototype.find()")}}</dd>
 <dt>{{jsxref("TypedArray.findIndex", "Uint32Array.prototype.findIndex()")}}</dt>
 <dd>如果数组中的元素满足提供的测试函数,返回找到的下标,如果没有找到则返回 -1。请参考 {{jsxref("Array.prototype.findIndex()")}}</dd>
 <dt>{{jsxref("TypedArray.forEach", "Uint32Array.prototype.forEach()")}}</dt>
 <dd>对数组内的每个元素调用一个函数。请参考 {{jsxref("Array.prototype.forEach()")}}</dd>
 <dt>{{jsxref("TypedArray.includes", "Uint32Array.prototype.includes()")}}</dt>
 <dd>判断该数组是否包含特定值,如果包含返回 <code>true</code>,否则返回<code>false</code>。请参考 {{jsxref("Array.prototype.includes()")}}</dd>
 <dt>{{jsxref("TypedArray.indexOf", "Uint32Array.prototype.indexOf()")}}</dt>
 <dd>返回数组中等于特定值的第一个元素(下标最小)的下标,如果没有找到则返回 -1。请参考 {{jsxref("Array.prototype.indexOf()")}}</dd>
 <dt>{{jsxref("TypedArray.join", "Uint32Array.prototype.join()")}}</dt>
 <dd>将数组内的元素拼接成一个字符串。请参考 {{jsxref("Array.prototype.join()")}}</dd>
 <dt>{{jsxref("TypedArray.keys", "Uint32Array.prototype.keys()")}}</dt>
 <dd>返回一个 <code>Array Iterator</code> 对象,该对象包含数组中所有索引(key)。请参考 {{jsxref("Array.prototype.keys()")}}</dd>
 <dt>{{jsxref("TypedArray.lastIndexOf", "Uint32Array.prototype.lastIndexOf()")}}</dt>
 <dd>返回数组中等于特定值的最后一个元素(下标最大)的下标,如果没有找到则返回 -1。请参考 {{jsxref("Array.prototype.lastIndexOf()")}}</dd>
 <dt>{{jsxref("TypedArray.map", "Uint32Array.prototype.map()")}}</dt>
 <dd>用该数组的每个元素调用给定函数的结果创建新数组。请参考 {{jsxref("Array.prototype.map()")}}</dd>
 <dt>{{jsxref("TypedArray.reduce", "Uint32Array.prototype.reduce()")}}</dt>
 <dd>对累加器和数组的每个值应用函数(从左到右),使其归约为单一的值。请参考 {{jsxref("Array.prototype.reduce()")}}</dd>
 <dt>{{jsxref("TypedArray.reduceRight", "Uint32Array.prototype.reduceRight()")}}</dt>
 <dd>对累加器和数组的每个值应用函数(从右到左),使其归约为单一的值。请参考 {{jsxref("Array.prototype.reduceRight()")}}</dd>
 <dt>{{jsxref("TypedArray.reverse", "Uint32Array.prototype.reverse()")}}</dt>
 <dd>翻转数组中的元素顺序——首尾颠倒。请参考 {{jsxref("Array.prototype.reverse()")}}</dd>
 <dt>{{jsxref("TypedArray.set", "Uint32Array.prototype.set()")}}</dt>
 <dd>从一个给定的数组中读取多个数据并存储至 typed array。</dd>
 <dt>{{jsxref("TypedArray.slice", "Uint32Array.prototype.slice()")}}</dt>
 <dd>提取数组的某个部分并返回新的数组。请参考 {{jsxref("Array.prototype.slice()")}}</dd>
 <dt>{{jsxref("TypedArray.some", "Uint32Array.prototype.some()")}}</dt>
 <dd>测试是否数组中有(至少一个)元素可通过给定的测试函数。请参考 {{jsxref("Array.prototype.some()")}}</dd>
 <dt>{{jsxref("TypedArray.sort", "Uint32Array.prototype.sort()")}}</dt>
 <dd>就地排序数组中的元素,并返回该数组。请参考 {{jsxref("Array.prototype.sort()")}}</dd>
 <dt>{{jsxref("TypedArray.subarray", "Uint32Array.prototype.subarray()")}}</dt>
 <dd>根据给定的起始和结束元素下标,返回一个新的 <code>Uint32Array</code> 子数组。</dd>
 <dt>{{jsxref("TypedArray.values", "Uint32Array.prototype.values()")}}</dt>
 <dd>返回新的 <code>Array Iterator</code> 对象,其含有数组中每个索引的值。请参考 {{jsxref("Array.prototype.values()")}}</dd>
 <dt>{{jsxref("TypedArray.toLocaleString", "Uint32Array.prototype.toLocaleString()")}}</dt>
 <dd>返回表示数组及其元素的本地化字符串。请参考 {{jsxref("Array.prototype.toLocaleString()")}}</dd>
 <dt>{{jsxref("TypedArray.toString", "Uint32Array.prototype.toString()")}}</dt>
 <dd>返回表示数组及其元素的字符串。请参考 {{jsxref("Array.prototype.toString()")}}</dd>
 <dt>{{jsxref("TypedArray.@@iterator", "Uint32Array.prototype[@@iterator]()")}}</dt>
 <dd>返回新的 <code>Array Iterator</code> 对象,其含有数组中每个索引的值。</dd>
</dl>

<h2 id="示例">示例</h2>

<p>用不同的方法创建 <code>Uint32Array</code></p>

<pre class="brush: js">// 给定长度
var uint32 = new Uint32Array(2);
uint32[0] = 42;
console.log(uint32[0]); // 42
console.log(uint32.length); // 2
console.log(uint32.BYTES_PER_ELEMENT); // 4

// 给定数组
var arr = new Uint32Array([21, 31]);
console.log(arr[1]); // 31

// 给定 TypedArray
var x = new Uint32Array([21, 31]);
var y = new Uint32Array(x);
console.log(y[0]); // 21

// 给定 ArrayBuffer
var 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]</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#table-49', 'TypedArray constructors')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div class="hidden">
<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
</div>

<p>{{Compat("javascript.builtins.Uint32Array")}}</p>

<h2 id="相关链接">相关链接</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Typed_arrays" title="en/JavaScript typed arrays">JavaScript typed arrays</a></li>
 <li>{{jsxref("ArrayBuffer")}}</li>
 <li>{{jsxref("DataView")}}</li>
</ul>