Float32Array
Float32Array
类型数组代表的是平台字节顺序为32位的浮点数型数组(对应于 C 浮点数据类型) 。 如果需要控制字节顺序, 使用DataView
替代。其内容初始化为0
。一旦建立起来,你可以使用这个对象的方法对其元素进行操作,或者使用标准数组索引语法 (使用方括号)。
语法
new Float32Array(); // new in ES2017
new Float32Array(length);
new Float32Array(typedArray);
new Float32Array(object);
new Float32Array(buffer [, byteOffset [, length]]);
更多的语法信息和参数,参见TypedArray。
属性
Float32Array.BYTES_PER_ELEMENT
返回元素字节数。 在Float32Array的情况下返回4。
Float32Array.length
长度属性的值为 3。关于其实际长度(元素数量)参见Float32Array.prototype.length
。
Float32Array.name
返回构造函数名字的字符串值。在Float32Array
类型的情况下为:"Float32Array"。
Float32Array.prototype
TypedArray对象的原型。
方法
Float32Array.from()
从一个类数组对象或可遍历对象创建一个新的Float32Array。参见Array.from()
。
Float32Array.of()
用可变数量的参数创建一个新的Float32Array。 参见Array.of()
。
Float32Array
属性
所有的Float32Array对象都
继承自 %TypedArray%.prototype
。
特性
Float32Array.prototype.constructor
返回创建这个实例原型的函数。 这是Float32Array
默认的构造函数。
Float32Array.prototype.buffer
只读返回这个Float32Array引用的
ArrayBuffer
。构造时已固定,所以是只读的。
Float32Array.prototype.byteLength
只读返回从Float32Array的
ArrayBuffer
开头开始的
长度 (以字节为单位) 。构造时已固定,所以是只读的。
Float32Array.prototype.byteOffset
只读返回从Float32Array的
ArrayBuffer
开头开始的偏移量
(以字节为单位) 。构造时已固定,所以是只读的。
Float32Array.prototype.length
只读返回Float32Array中的元素个数
。构造时已固定,所以是只读的。
方法
Float32Array.prototype.copyWithin()
从数组复制元素。参见Array.prototype.copyWithin()
。
Float32Array.prototype.entries()
返回一个包含数组中每个元素键值对的数组遍历器对象
。参见Array.prototype.entries()
。
Float32Array.prototype.every()
检测是否所有元素都能通过给定函数的测试。参见Array.prototype.every()
。
Float32Array.prototype.fill()
用一个静态值填充给定的起始位置。 参见Array.prototype.fill()
。
Float32Array.prototype.filter()
创建一个新数组,数据为原数组中所有能让给入函数返回true的元素。参见Array.prototype.filter()
。
Float32Array.prototype.find()
返回满足测试函数的值,如果没有找到,返回undefined。 参见Array.prototype.find()
。
Float32Array.prototype.findIndex()
返回满足测试函数的值的位置,如果没有找到,返回-1。参见Array.prototype.findIndex()
。
Float32Array.prototype.forEach()
以每个元素为参数各调用一次函数。参见Array.prototype.forEach()
。
Float32Array.prototype.includes()
判断是否包含某个元素,返回true
或false
。参见Array.prototype.includes()
。
Float32Array.prototype.indexOf()
返回数组中等于给定值的元素的第一个(最小)位置, 没有找到则返回-1。参见Array.prototype.indexOf()
。
Float32Array.prototype.join()
合并所有数组元素到一个字符串中。 参见Array.prototype.join()
。
Float32Array.prototype.keys()
返回一个包含数组中所有索引的数组遍历器
。 参见Array.prototype.keys()
。
Float32Array.prototype.lastIndexOf()
返回数组中等于给定值的元素的最后(最大)位置, 没有找到则返回-1。参见Array.prototype.lastIndexOf()
。
Float32Array.prototype.map()
创建一个新的数组,数据由原数组每个元素依次传入给定函数后返回的值组成。参见Array.prototype.map()
。
Float32Array.prototype.move()
未实现Float32Array.prototype.copyWithin()
以前的一个非标准版本。
Float32Array.prototype.reduce()
传入一个函数作为累加器,从左到右遍历,最终得到一个值。 参见Array.prototype.reduce()
。
Float32Array.prototype.reduceRight()
传入一个函数作为累加器,从右到左遍历,最终得到一个值。参见Array.prototype.reduceRight()
。
Float32Array.prototype.reverse()
反转数组元素的顺序 — 第一个变为最后一个, 最后一个变为第一个。参见Array.prototype.reverse()
。
Float32Array.prototype.set()
从给定的数组存入多个数值。
Float32Array.prototype.slice()
提取数组的一部分并且返回一个新数组。参见Array.prototype.slice()
。
Float32Array.prototype.some()
如果数组中至少有一个元素满足测试函数的要求则返回true。参见Array.prototype.some()
。
Float32Array.prototype.sort()
对数组元素进行排序并返回数组。参见Array.prototype.sort()
。
Float32Array.prototype.subarray()
从给定的起始位置返回一个新的Float32Array
。
Float32Array.prototype.values()
返回一个包含所有数组元素的数组遍历器对象。 参见Array.prototype.values()
。
Float32Array.prototype.toLocaleString()
返回一个代表数组和其元素的本地化格式字符串。参见Array.prototype.toLocaleString()
。
Float32Array.prototype.toString()
返回一个代表数组和它的元素的字符串。参见Array.prototype.toString()
。
Float32Array.prototype[@@iterator]()
返回一个新的包含数组元素的数组迭代器对象。
示例
不同方式创建 Float32Array
:
// 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);
// From an iterable
var iterable = function*(){ yield* [1,2,3]; }();
var float32 = new Float32Array(iterable);
// Float32Array[1, 2, 3]
规范
Specification | Status | Comment |
---|---|---|
Typed Array Specification | Obsolete | Superseded by ECMAScript 2015. |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'TypedArray constructors' in that specification. | Standard | Initial definition in an ECMA standard. Specified that new is required. |
ECMAScript Latest Draft (ECMA-262)The definition of 'TypedArray constructors' in that specification. | Draft | ECMAScript 2017 changed the Float32Array constructor to use the ToIndex operation and allows constructors with no arguments. |
浏览器兼容性
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 7.0 | (Yes) | 4.0 (2) | 10 | 11.6 | 5.1 |
new is required | ? | ? | 44 (44) | ? | ? | ? |
Iterable in constructor | ? | ? | 52 (52) | ? | ? | ? |
Constructor without arguments | ? | ? | 55 (55) | ? | ? | ? |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | 4.0 | (Yes) | (Yes) | 4.0 (2) | 10 | 11.6 | 4.2 |
new is required | ? | ? | ? | 44.0 (44) | ? | ? | ? |
Iterable in constructor | ? | ? | ? | 52.0 (52) | ? | ? | ? |
Constructor without arguments | ? | ? | ? | 55.0 (55) | ? | ? | ? |
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com