aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/uint8clampedarray/index.html
blob: f1fe65cc7507c43771c588be498bab04a25d6a44 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
---
title: Uint8ClampedArray
slug: Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray
translation_of: Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray
---
<div>{{JSRef}}</div>

<p><strong><code>Uint8ClampedArray</code>(8位无符号整型固定数组)</strong> 类型化数组表示一个由值固定在0-255区间的8位无符号整型组成的数组;如果你指定一个在 [0,255] 区间外的值,它将被替换为0或255;如果你指定一个非整数,那么它将被设置为最接近它的整数。(数组)内容被初始化为0。一旦(数组)被创建,你可以使用对象的方法引用数组里的元素,或使用标准的数组索引语法(即使用方括号标记)。</p>

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

<pre class="syntaxbox">new Uint8ClampedArray(length);
new Uint8ClampedArray(typedArray);
new Uint8ClampedArray(object);
new Uint8ClampedArray(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", "Uint8ClampedArray.BYTES_PER_ELEMENT")}}</dt>
 <dd>返回元素大小的一个数值。对 <code>Uint8ClampedArray</code> 而言是1。</dd>
 <dt>{{jsxref("TypedArray.prototype.length", "Uint8ClampedArray.prototype.length")}}</dt>
 <dd>静态长度属性值为0。对于实际长度(元素的数量),见 {{jsxref("TypedArray.prototype.length", "Uint8ClampedArray.prototype.length")}}</dd>
 <dt>{{jsxref("TypedArray.name", "Uint8ClampedArray.name")}}</dt>
 <dd>返回构造函数名的字符串值。对 <code>Uint8ClampedArray</code> 类型而言:"Uint8ClampedArray"。</dd>
 <dt>{{jsxref("TypedArray.prototype", "Uint8ClampedArray.prototype")}}</dt>
 <dd>原型是 <em>TypedArray</em> (类型化数组)对象。</dd>
</dl>

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

<dl>
 <dt>{{jsxref("TypedArray.from", "Uint8ClampedArray.from()")}}</dt>
 <dd>从一个类数组或可枚举对象创建一个新的 <code>Uint8ClampedArray</code>。参见 {{jsxref("Array.from()")}}</dd>
 <dt>{{jsxref("TypedArray.of", "Uint8ClampedArray.of()")}}</dt>
 <dd>通过一个可选数量参数来创建一个新的 <code>Uint8ClampedArray</code>。参见 {{jsxref("Array.of()")}}</dd>
</dl>

<h2 id="Uint8ClampedArray_原型">Uint8ClampedArray 原型</h2>

<p>所有的 <code>Uint8ClampedArray</code> 对象继承自 {{jsxref("TypedArray.prototype", "%TypedArray%.prototype")}}</p>

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

<dl>
 <dt><code>Uint8ClampedArray.prototype.constructor</code></dt>
 <dd>返回创建一个实例原型的函数。这是 <code>Uint8ClampedArray</code> 默认的构造函数。</dd>
 <dt>{{jsxref("TypedArray.prototype.buffer", "Uint8ClampedArray.prototype.buffer")}} {{readonlyInline}}</dt>
 <dd>返回由 <code>Uint8ClampedArray</code> 引用的 {{jsxref("ArrayBuffer")}} 。在创建时所固定下来,因此<strong>只能读取</strong></dd>
 <dt>{{jsxref("TypedArray.prototype.byteLength", "Uint8ClampedArray.prototype.byteLength")}} {{readonlyInline}}</dt>
 <dd>返回从 {{jsxref("ArrayBuffer")}} 开始的 <code>Uint8ClampedArray</code> 的(字节的)长度。在创建时所固定下来,因此<strong>只能读取</strong></dd>
 <dt>{{jsxref("TypedArray.prototype.byteOffset", "Uint8ClampedArray.prototype.byteOffset")}} {{readonlyInline}}</dt>
 <dd>返回从 {{jsxref("ArrayBuffer")}} 开始的 <code>Uint8ClampedArray</code> 的(字节的)偏移。在创建时所固定下来,因此<strong>只能读取</strong></dd>
 <dt>{{jsxref("TypedArray.prototype.length", "Uint8ClampedArray.prototype.length")}} {{readonlyInline}}</dt>
 <dd>返回 <code>UintClamped8Array</code> 具有的元素数量。在创建时所固定下来,因此<strong>只能读取</strong></dd>
</dl>

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

<dl>
 <dt>{{jsxref("TypedArray.copyWithin", "Uint8ClampedArray.prototype.copyWithin()")}}</dt>
 <dd>复制数组内一段数组元素的序列。参见 {{jsxref("Array.prototype.copyWithin()")}}</dd>
 <dt>{{jsxref("TypedArray.entries", "Uint8ClampedArray.prototype.entries()")}}</dt>
 <dd>返回一个新的包含数组中每个索引对应的键/值对的数组迭代器对象。参见 {{jsxref("Array.prototype.entries()")}}</dd>
 <dt>{{jsxref("TypedArray.every", "Uint8ClampedArray.prototype.every()")}}</dt>
 <dd>测试数组里的所有元素是否通过所提供的函数的测试。参见 {{jsxref("Array.prototype.every()")}}</dd>
 <dt>{{jsxref("TypedArray.fill", "Uint8ClampedArray.prototype.fill()")}}</dt>
 <dd>用一个固定值填充一个数组内的从起始索引到结束索引的全部元素。参见 {{jsxref("Array.prototype.fill()")}}</dd>
 <dt>{{jsxref("TypedArray.filter", "Uint8ClampedArray.prototype.filter()")}}</dt>
 <dd>由该数组中所有经所提供的筛选函数返回为 true 的元素创建一个新数组。参见 {{jsxref("Array.prototype.filter()")}}</dd>
 <dt>{{jsxref("TypedArray.find", "Uint8ClampedArray.prototype.find()")}}</dt>
 <dd>如果数组里的一个元素符合所提供的测试函数则返回找到的这个值,如果没有找到则返回 <code>undefined</code>。参见 {{jsxref("Array.prototype.find()")}}</dd>
 <dt>{{jsxref("TypedArray.findIndex", "Uint8ClampedArray.prototype.findIndex()")}}</dt>
 <dd>如果数组里的一个元素符合所提供的测试函数则返回找到的索引,如果没有找到则返回 -1。参见 {{jsxref("Array.prototype.findIndex()")}}</dd>
 <dt>{{jsxref("TypedArray.forEach", "Uint8ClampedArray.prototype.forEach()")}}</dt>
 <dd>对数组内的每个元素调用一个函数。参见 {{jsxref("Array.prototype.forEach()")}}</dd>
 <dt>{{jsxref("TypedArray.includes", "Uint8ClampedArray.prototype.includes()")}} {{experimental_inline}}</dt>
 <dd>确定一个类型化数组是否包含一个特定的元素,对应地返回 <code>true</code> 或 <code>false</code>。参见 {{jsxref("Array.prototype.includes()")}}</dd>
 <dt>{{jsxref("TypedArray.indexOf", "Uint8ClampedArray.prototype.indexOf()")}}</dt>
 <dd>Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found. See also {{jsxref("Array.prototype.indexOf()")}}.</dd>
 <dt>{{jsxref("TypedArray.join", "Uint8ClampedArray.prototype.join()")}}</dt>
 <dd>Joins all elements of an array into a string. See also {{jsxref("Array.prototype.join()")}}.</dd>
 <dt>{{jsxref("TypedArray.keys", "Uint8ClampedArray.prototype.keys()")}}</dt>
 <dd>Returns a new <code>Array Iterator</code> that contains the keys for each index in the array. See also {{jsxref("Array.prototype.keys()")}}.</dd>
 <dt>{{jsxref("TypedArray.lastIndexOf", "Uint8ClampedArray.prototype.lastIndexOf()")}}</dt>
 <dd>Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found. See also {{jsxref("Array.prototype.lastIndexOf()")}}.</dd>
 <dt>{{jsxref("TypedArray.map", "Uint8ClampedArray.prototype.map()")}}</dt>
 <dd>Creates a new array with the results of calling a provided function on every element in this array. See also {{jsxref("Array.prototype.map()")}}.</dd>
 <dt>{{jsxref("TypedArray.move", "Uint8ClampedArray.prototype.move()")}} {{non-standard_inline}} {{unimplemented_inline}}</dt>
 <dd>Former non-standard version of {{jsxref("TypedArray.copyWithin", "Uint8ClampedArray.prototype.copyWithin()")}}.</dd>
 <dt>{{jsxref("TypedArray.reduce", "Uint8ClampedArray.prototype.reduce()")}}</dt>
 <dd>Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value. See also {{jsxref("Array.prototype.reduce()")}}.</dd>
 <dt>{{jsxref("TypedArray.reduceRight", "Uint8ClampedArray.prototype.reduceRight()")}}</dt>
 <dd>Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value. See also {{jsxref("Array.prototype.reduceRight()")}}.</dd>
 <dt>{{jsxref("TypedArray.reverse", "Uint8ClampedArray.prototype.reverse()")}}</dt>
 <dd>Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first. See also {{jsxref("Array.prototype.reverse()")}}.</dd>
 <dt>{{jsxref("TypedArray.set", "Uint8ClampedArray.prototype.set()")}}</dt>
 <dd>Stores multiple values in the typed array, reading input values from a specified array.</dd>
 <dt>{{jsxref("TypedArray.slice", "Uint8ClampedArray.prototype.slice()")}}</dt>
 <dd>Extracts a section of an array and returns a new array. See also {{jsxref("Array.prototype.slice()")}}.</dd>
 <dt>{{jsxref("TypedArray.some", "Uint8ClampedArray.prototype.some()")}}</dt>
 <dd>Returns true if at least one element in this array satisfies the provided testing function. See also {{jsxref("Array.prototype.some()")}}.</dd>
 <dt>{{jsxref("TypedArray.sort", "Uint8ClampedArray.prototype.sort()")}}</dt>
 <dd>Sorts the elements of an array in place and returns the array. See also {{jsxref("Array.prototype.sort()")}}.</dd>
 <dt>{{jsxref("TypedArray.subarray", "Uint8ClampedArray.prototype.subarray()")}}</dt>
 <dd>Returns a new <code>Uint8ClampedArray</code> from the given start and end element index.</dd>
 <dt>{{jsxref("TypedArray.values", "Uint8ClampedArray.prototype.values()")}}</dt>
 <dd>Returns a new <code>Array Iterator</code> object that contains the values for each index in the array. See also {{jsxref("Array.prototype.values()")}}.</dd>
 <dt>{{jsxref("TypedArray.toLocaleString", "Uint8ClampedArray.prototype.toLocaleString()")}}</dt>
 <dd>Returns a localized string representing the array and its elements. See also {{jsxref("Array.prototype.toLocaleString()")}}.</dd>
 <dt>{{jsxref("TypedArray.toString", "Uint8ClampedArray.prototype.toString()")}}</dt>
 <dd>Returns a string representing the array and its elements. See also {{jsxref("Array.prototype.toString()")}}.</dd>
 <dt>{{jsxref("TypedArray.@@iterator", "Uint8ClampedArray.prototype[@@iterator]()")}}</dt>
 <dd>Returns a new <code>Array Iterator</code> object that contains the values for each index in the array.</dd>
</dl>

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

<p>创建一个 <code>Uint8ClampedArray</code> 的不同方式:</p>

<pre class="brush: js">// From a length
var uintc8 = new Uint8ClampedArray(2);
uintc8[0] = 42;
uintc8[1] = 1337;
console.log(uintc8[0]); // 42
console.log(uintc8[1]); // 255 (clamped)
console.log(uintc8.length); // 2
console.log(uintc8.BYTES_PER_ELEMENT); // 1

// From an array
var arr = new Uint8ClampedArray([21,31]);
console.log(arr[1]); // 31

// From another TypedArray
var x = new Uint8ClampedArray([21, 31]);
var y = new Uint8ClampedArray(x);
console.log(y[0]); // 21

// From an ArrayBuffer
var buffer = new ArrayBuffer(8);
var z = new Uint8ClampedArray(buffer, 1, 4);

// From an iterable
var iterable = function*(){ yield* [1,2,3]; }();
var uintc8 = new Uint8ClampedArray(iterable);
// Uint8ClampedArray[1, 2, 3]
</pre>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">注释</th>
  </tr>
  <tr>
   <td>{{SpecName('Typed Array')}}</td>
   <td>{{Spec2('Typed Array')}}</td>
   <td>已由 ECMAScript 2015 替代。</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#table-49', 'TypedArray constructors')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>最初定义在一份 ECMA 标准中。规定 <code>new</code> 是必需的。</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#table-49', 'TypedArray constructors')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

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

<p>{{CompatibilityTable}}</p>

<div>
<table class="compat-table">
 <tbody>
  <tr>
   <th>特性</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>基础支持</td>
   <td>7.0</td>
   <td>{{ CompatGeckoDesktop(2) }}</td>
   <td>11 (as of <a href="https://support.microsoft.com/en-us/kb/2929437">KB2929437</a>)</td>
   <td>11.6</td>
   <td>5.1</td>
  </tr>
  <tr>
   <td>需要使用 <code>new</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{ CompatGeckoDesktop(44) }}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>构造函数中可枚举</td>
   <td>{{CompatUnknown}}</td>
   <td>{{ CompatGeckoDesktop(52) }}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div>
<table class="compat-table">
 <tbody>
  <tr>
   <th>特性</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>基础支持</td>
   <td>4.0</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{ CompatGeckoMobile(2) }}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>11.6</td>
   <td>4.2</td>
  </tr>
  <tr>
   <td>需要使用 <code>new</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{ CompatGeckoMobile(44) }}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>构造函数中可枚举</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{ CompatGeckoMobile(52) }}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="兼容性注意事项">兼容性注意事项</h2>

<p>从 ECMAScript 2015 开始, <code>Uint8ClampedArray</code> 构造函数需要用一个 {{jsxref("Operators/new", "new")}} 操作符来构建。从现在开始,不使用 <code>new</code> 来调用一个 <code>Uint8ClampedArray</code> 构造函数将会抛出一个 {{jsxref("TypeError")}}</p>

<pre class="brush: js example-bad">var dv = Uint8ClampedArray([1, 2, 3]);
// TypeError: calling a builtin Uint8ClampedArray constructor
// without new is forbidden</pre>

<pre class="brush: js example-good">var dv = new Uint8ClampedArray([1, 2, 3]);</pre>

<h2 id="参见">参见</h2>

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