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
|
---
title: Float32Array
slug: Web/JavaScript/Reference/Global_Objects/Float32Array
tags:
- JavaScript
- TypeArray
- TypeArrays
- 构造函数
translation_of: Web/JavaScript/Reference/Global_Objects/Float32Array
---
<div>{{JSRef}}</div>
<p><strong><code>Float32Array</code></strong> 类型数组代表的是平台字节顺序为32位的浮点数型数组(对应于 C 浮点数据类型) 。 如果需要控制字节顺序, 使用 {{jsxref("DataView")}} 替代。其内容初始化为<code>0</code>。一旦建立起来,你可以使用这个对象的方法对其元素进行操作,或者使用标准数组索引语法 (使用方括号)。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">new Float32Array(length);
new Float32Array(typedArray);
new Float32Array(object);
new Float32Array(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", "Float32Array.BYTES_PER_ELEMENT")}}</dt>
<dd>返回元素字节数。 <code>在</code> <code>Float32Array的情况下返回4。</code></dd>
<dt>Float32Array.length</dt>
<dd>长度属性的值为 3。关于其实际长度(元素数量)参见{{jsxref("TypedArray.prototype.length", "Float32Array.prototype.length")}}。</dd>
<dt>{{jsxref("TypedArray.name", "Float32Array.name")}}</dt>
<dd>返回构造函数名字的字符串值。在 <code>Float32Array</code> 类型的情况下为:"Float32Array"。</dd>
<dt>{{jsxref("TypedArray.prototype", "Float32Array.prototype")}}</dt>
<dd> <em>TypedArray</em>对象的原型。</dd>
</dl>
<h2 id="方法">方法</h2>
<dl>
<dt>{{jsxref("TypedArray.from", "Float32Array.from()")}}</dt>
<dd>从一个类数组对象或可遍历对象创建一个新的Float32Array。参见 {{jsxref("Array.from()")}}。</dd>
<dt>{{jsxref("TypedArray.of", "Float32Array.of()")}}</dt>
<dd>用可变数量的参数创建一个新的Float32Array。 参见 {{jsxref("Array.of()")}}。</dd>
</dl>
<h2 id="Boolean_instances"><code>Float32Array</code> 属性</h2>
<p>所有的<code>Float32Array对象都</code>继承自 {{jsxref("TypedArray.prototype", "%TypedArray%.prototype")}}。</p>
<h3 id="特性">特性</h3>
<dl>
<dt><code>Float32Array.prototype.constructor</code></dt>
<dd>返回创建这个实例原型的函数。 这是<code>Float32Array</code> 默认的构造函数。</dd>
<dt>{{jsxref("TypedArray.prototype.buffer", "Float32Array.prototype.buffer")}} {{readonlyInline}}</dt>
<dd>返回这个<code>Float32Array引用的</code>{{jsxref("ArrayBuffer")}}。构造时已固定,所以是<strong>只读</strong>的。</dd>
<dt>{{jsxref("TypedArray.prototype.byteLength", "Float32Array.prototype.byteLength")}} {{readonlyInline}}</dt>
<dd>返回从<code>Float32Array的</code>{{jsxref("ArrayBuffer")}}开头开始<code>的</code>长度 (以字节为单位) 。构造时已固定,所以是<strong>只读</strong>的。</dd>
<dt>{{jsxref("TypedArray.prototype.byteOffset", "Float32Array.prototype.byteOffset")}} {{readonlyInline}}</dt>
<dd>返回从<code>Float32Array的</code>{{jsxref("ArrayBuffer")}}开头开始<code>的偏移量</code> (以字节为单位) 。构造时已固定,所以是<strong>只读</strong>的。</dd>
<dt>{{jsxref("TypedArray.prototype.length", "Float32Array.prototype.length")}} {{readonlyInline}}</dt>
<dd>返回<code>Float32Array中的元素个数</code>。构造时已固定,所以是<strong>只读</strong>的。</dd>
</dl>
<h3 id="方法_2">方法</h3>
<dl>
<dt>{{jsxref("TypedArray.copyWithin", "Float32Array.prototype.copyWithin()")}}</dt>
<dd>从数组复制元素。参见{{jsxref("Array.prototype.copyWithin()")}}。</dd>
<dt>{{jsxref("TypedArray.entries", "Float32Array.prototype.entries()")}}</dt>
<dd><code>返回一个包含数组中每个元素键值对的数组遍历器对象</code>。参见{{jsxref("Array.prototype.entries()")}}。</dd>
<dt>{{jsxref("TypedArray.every", "Float32Array.prototype.every()")}}</dt>
<dd>检测是否所有元素都能通过给定函数的测试。参见{{jsxref("Array.prototype.every()")}}。</dd>
<dt>{{jsxref("TypedArray.fill", "Float32Array.prototype.fill()")}}</dt>
<dd>用一个静态值填充给定的起始位置。 参见{{jsxref("Array.prototype.fill()")}}。</dd>
<dt>{{jsxref("TypedArray.filter", "Float32Array.prototype.filter()")}}</dt>
<dd>创建一个新数组,数据为原数组中所有能让给入函数返回true的元素。参见{{jsxref("Array.prototype.filter()")}}。</dd>
<dt>{{jsxref("TypedArray.find", "Float32Array.prototype.find()")}}</dt>
<dd>返回满足测试函数的值,如果没有找到,返回undefined。 参见{{jsxref("Array.prototype.find()")}}。</dd>
<dt>{{jsxref("TypedArray.findIndex", "Float32Array.prototype.findIndex()")}}</dt>
<dd>返回满足测试函数的值的位置,如果没有找到,返回-1。参见{{jsxref("Array.prototype.findIndex()")}}。</dd>
<dt>{{jsxref("TypedArray.forEach", "Float32Array.prototype.forEach()")}}</dt>
<dd>以每个元素为参数各调用一次函数。参见{{jsxref("Array.prototype.forEach()")}}。</dd>
<dt>{{jsxref("TypedArray.includes", "Float32Array.prototype.includes()")}} {{experimental_inline}}</dt>
<dd>判断是否包含某个元素,<code>返回true</code> 或 <code>false</code>。参见{{jsxref("Array.prototype.includes()")}}。</dd>
<dt>{{jsxref("TypedArray.indexOf", "Float32Array.prototype.indexOf()")}}</dt>
<dd>返回数组中等于给定值的元素的第一个(最小)位置, 没有找到则返回-1。参见{{jsxref("Array.prototype.indexOf()")}}。</dd>
<dt>{{jsxref("TypedArray.join", "Float32Array.prototype.join()")}}</dt>
<dd>合并所有数组元素到一个字符串中。 参见{{jsxref("Array.prototype.join()")}}。</dd>
<dt>{{jsxref("TypedArray.keys", "Float32Array.prototype.keys()")}}</dt>
<dd><code>返回一个包含数组中所有索引的数组遍历器</code>。 参见{{jsxref("Array.prototype.keys()")}}。</dd>
<dt>{{jsxref("TypedArray.lastIndexOf", "Float32Array.prototype.lastIndexOf()")}}</dt>
<dd>返回数组中等于给定值的元素的最后(最大)位置, 没有找到则返回-1。参见{{jsxref("Array.prototype.lastIndexOf()")}}。</dd>
<dt>{{jsxref("TypedArray.map", "Float32Array.prototype.map()")}}</dt>
<dd>创建一个新的数组,数据由原数组每个元素依次传入给定函数后返回的值组成。参见{{jsxref("Array.prototype.map()")}}。</dd>
<dt>{{jsxref("TypedArray.move", "Float32Array.prototype.move()")}} {{non-standard_inline}} {{unimplemented_inline}}</dt>
<dd>{{jsxref("TypedArray.copyWithin", "Float32Array.prototype.copyWithin()")}}以前的一个非标准版本。</dd>
<dt>{{jsxref("TypedArray.reduce", "Float32Array.prototype.reduce()")}}</dt>
<dd>传入一个函数作为累加器,从左到右遍历,最终得到一个值。 参见{{jsxref("Array.prototype.reduce()")}}。</dd>
<dt>{{jsxref("TypedArray.reduceRight", "Float32Array.prototype.reduceRight()")}}</dt>
<dd>传入一个函数作为累加器,从右到左遍历,最终得到一个值。参见{{jsxref("Array.prototype.reduceRight()")}}。</dd>
<dt>{{jsxref("TypedArray.reverse", "Float32Array.prototype.reverse()")}}</dt>
<dd>反转数组元素的顺序 — 第一个变为最后一个, 最后一个变为第一个。参见{{jsxref("Array.prototype.reverse()")}}。</dd>
<dt>{{jsxref("TypedArray.set", "Float32Array.prototype.set()")}}</dt>
<dd>从给定的数组存入多个数值。</dd>
<dt>{{jsxref("TypedArray.slice", "Float32Array.prototype.slice()")}}</dt>
<dd>提取数组的一部分并且返回一个新数组。参见{{jsxref("Array.prototype.slice()")}}。</dd>
<dt>{{jsxref("TypedArray.some", "Float32Array.prototype.some()")}}</dt>
<dd>如果数组中至少有一个元素满足测试函数的要求则返回true。参见{{jsxref("Array.prototype.some()")}}。</dd>
<dt>{{jsxref("TypedArray.sort", "Float32Array.prototype.sort()")}}</dt>
<dd>对数组元素进行排序并返回数组。参见{{jsxref("Array.prototype.sort()")}}。</dd>
<dt>{{jsxref("TypedArray.subarray", "Float32Array.prototype.subarray()")}}</dt>
<dd>从给定的起始位置返回一个新的<code>Float32Array</code> 。</dd>
<dt>{{jsxref("TypedArray.values", "Float32Array.prototype.values()")}}</dt>
<dd>返回一个包含所有数组元素的数组遍历器对象。 参见{{jsxref("Array.prototype.values()")}}。</dd>
<dt>{{jsxref("TypedArray.toLocaleString", "Float32Array.prototype.toLocaleString()")}}</dt>
<dd>返回一个代表数组和其元素的本地化格式字符串。参见{{jsxref("Array.prototype.toLocaleString()")}}。</dd>
<dt>{{jsxref("TypedArray.toString", "Float32Array.prototype.toString()")}}</dt>
<dd>返回一个代表数组和它的元素的字符串。参见{{jsxref("Array.prototype.toString()")}}。</dd>
<dt>{{jsxref("TypedArray.@@iterator", "Float32Array.prototype[@@iterator]()")}}</dt>
<dd>返回一个新的包含数组元素的数组迭代器对象。</dd>
</dl>
<h2 id="例子">例子</h2>
<pre class="brush: js">// From a length
var float32 = new Float32Array(2);
float32[0] = 42;
console.log(float32[0]); // 42
console.log(float32.length); // 2
console.log(float32.BYTES_PER_ELEMENT); // 4
// From an array
var arr = new Float32Array([21,31]);
console.log(arr[1]); // 31
// From another TypedArray
var x = new Float32Array([21, 31]);
var y = new Float32Array(x);
console.log(y[0]); // 21
// From an ArrayBuffer
var buffer = new ArrayBuffer(16);
var z = new Float32Array(buffer, 0, 4);
</pre>
<h2 id="sect1"> </h2>
<h2 id="规范">规范</h2>
<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 6取代</td>
</tr>
<tr>
<td>{{SpecName('ES6', '#table-49', 'TypedArray constructors')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>在ECMA中初始定义,另外规定需要使用new。</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#table-49', 'TypedArray constructors')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
{{Compat}}
<h2 id="一致性提示">一致性提示</h2>
<p>从ECMAScript 2015 (ES6)开始, <code>Float32Array</code>构造函数需要用一个{{jsxref("Operators/new", "new")}}操作符来构造。现在直接把<code>Float32Array构造函数当函数调用而不使用new,会抛出一个</code>{{jsxref("TypeError")}}。</p>
<pre class="brush: js example-bad">var dv = Float32Array([1, 2, 3]);
// TypeError: calling a builtin Float32Array constructor
// 不允许不使用new</pre>
<pre class="brush: js example-good">var dv = new Float32Array([1, 2, 3]);</pre>
<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>
|