在线文档教程

Float32Array

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.from()从一个类数组对象或可遍历对象创建一个新的Float32Array。参见Array.from()

Float32Array 属性

所有的Float32Array对象都继承自 %TypedArray%.prototype

特性

Float32Array.prototype.constructor返回创建这个实例原型的函数。 这是Float32Array 默认的构造函数。

方法

Float32Array.prototype.copyWithin()从数组复制元素。参见Array.prototype.copyWithin()

示例

不同方式创建 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]

规范

SpecificationStatusComment
Typed Array SpecificationObsoleteSuperseded by ECMAScript 2015.
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'TypedArray constructors' in that specification.StandardInitial definition in an ECMA standard. Specified that new is required.
ECMAScript Latest Draft (ECMA-262)The definition of 'TypedArray constructors' in that specification.DraftECMAScript 2017 changed the Float32Array constructor to use the ToIndex operation and allows constructors with no arguments.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support7.0(Yes)4.0 (2)1011.65.1
new is required??44 (44)???
Iterable in constructor??52 (52)???
Constructor without arguments??55 (55)???

FeatureAndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support4.0(Yes)(Yes)4.0 (2)1011.64.2
new is required???44.0 (44)???
Iterable in constructor???52.0 (52)???
Constructor without arguments???55.0 (55)???