非常教程

JavaScript参考手册

TypedArray

TypedArray

TypedArray对象描述底层的二进制数据缓冲的阵列状的图。没有命名的TypedArray全局属性,也没有直接可见的TypedArray构造函数。相反,有许多不同的全局属性,其值是特定元素类型的类型化数组构造函数,如下所示。在下面的页面中,您将找到可用于包含任何类型元素的任何类型数组的常用属性和方法。

语法

new TypedArray(); // new in ES2017
new TypedArray(length);
new TypedArray(typedArray);
new TypedArray(object);
new TypedArray(buffer [, byteOffset [, length]]);

where TypedArray() is one of:

Int8Array();
Uint8Array();
Uint8ClampedArray();
Int16Array();
Uint16Array();
Int32Array();
Uint32Array();
Float32Array();
Float64Array();

参数

length使用length参数调用时,将在内存中创建一个内部数组缓冲区,其大小的长度乘以BYTES_PER_ELEMENT个字节,其中包含zeros.typedArray当使用typedArray参数调用时,参数可以是任何类型化数组类型的对象(如Int32Array),typedArray被复制到一个新的类型数组中。在typedArray复制到新数组之前,每个值都将转换为相应的构造函数类型。然后,新的typedArray对象的长度将与typedArray参数的长度相同。对象当使用参数调用时object,将创建一个新的类型数组,如同通过method.buffer,byteOffset,lengthWhen使用a ,以及可选的a 和aTypedArray.from()bufferbyteOffsetlength参数,创建一个新的类型数组视图来查看指定的ArrayBuffer。的byteOffsetlength参数指定将通过类型数组视图被暴露的存储器范围。如果两者均被忽略,则全部buffer被查看; 如果仅length省略,buffer则查看其余部分。

描述

ECMAScript 2015定义了一个TypedArray构造函数,用作[[Prototype]]所有TypedArray构造函数的构造函数。这个构造函数没有直接暴露:没有全局%TypedArray%TypedArray属性。它只能通过Object.getPrototypeOf(Int8Array)和类似的方式直接访问。所有TypedArray_s构造函数都从%TypedArray%构造函数继承公共属性此外,所有类型数组原型(_TypedArray.prototype)有%TypedArray%.prototype他们的[[Prototype]]

%TypedArray%对自己的构造不是特别有用。在new表达式中调用它或使用它将抛出一个TypeError,除非在支持子类的JS引擎中创建对象时使用。目前没有这样的引擎,所以%TypedArray%仅对所有TypedArray构造函数的polyfill函数或属性有用。

当创建一个TypedArray(例如Int8Array)的实例时,在内存中创建一个数组缓冲区,或者,如果ArrayBuffer对象是作为构造函数参数给出的,那么就使用它。缓冲区地址被保存为实例的内部属性以及%%的所有方法TypedArrayprototype,即设置值和获取值等,对该阵列缓冲区地址进行操作。

访问特性

您可以使用标准数组索引语法引用数组中的元素(即使用括号表示法)。但是,在类型数组上获取或设置索引属性不会在原型链中搜索此属性,即使索引超出范围也是如此。索引属性将查询ArrayBuffer并永远不会查看对象属性。您仍然可以像使用所有对象一样使用命名属性。

// Setting and getting using standard array syntax
var int16 = new Int16Array(2);
int16[0] = 42;
console.log(int16[0]); // 42

// Indexed properties on prototypes are not consulted (Fx 25)
Int8Array.prototype[20] = 'foo';
(new Int8Array(32))[20]; // 0
// even when out of bound
Int8Array.prototype[20] = 'foo';
(new Int8Array(8))[20]; // undefined
// or with negative integers
Int8Array.prototype[-1] = 'foo';
(new Int8Array(8))[-1]; // undefined

// Named properties are allowed, though (Fx 30)
Int8Array.prototype.foo = 'bar';
(new Int8Array(32)).foo; // "bar"

TypedArray对象

Type

Size in bytes

Description

Web IDL type

Equivalent C type

Int8Array

1

8-bit two's complement signed integer

byte

int8_t

Uint8Array

1

8-bit unsigned integer

octet

uint8_t

Uint8ClampedArray

1

8-bit unsigned integer (clamped)

octet

uint8_t

Int16Array

2

16-bit two's complement signed integer

short

int16_t

Uint16Array

2

16-bit unsigned integer

unsigned short

uint16_t

Int32Array

4

32-bit two's complement signed integer

long

int32_t

Uint32Array

4

32-bit unsigned integer

unsigned long

uint32_t

Float32Array

4

32-bit IEEE floating point number

unrestricted float

float

Float64Array

8

64-bit IEEE floating point number

unrestricted double

double

属性

TypedArray.BYTES_PER_ELEMENT返回不同类型的数组对象的元素大小的数字值。TypedArray .lengthLength属性,其值为0. TypedArray.name返回构造函数名称的字符串值。例如“Int8Array”。get TypedArray[@@species]用于创建派生对象的构造函数,TypedArray对象的TypedArray.prototype原型。

方法

TypedArray.from()从类似数组或类的对象中创建一个新的类型数组。另见Array.from()TypedArray.of()用可变数量的参数创建一个新的类型化数组。另见Array.of()

TypedArray原型

所有_TypedArray_s继承自TypedArray.prototype

属性

TypedArray.prototype.constructor返回创建实例原型的函数。这是默认情况下相应的类型化数组类型函数。TypedArray.prototype.buffer只读返回ArrayBuffer类型数组引用的内容。在实施时固定,因此只读TypedArray.prototype.byteLength只读返回类型数组从其开始的长度(以字节为单位)ArrayBuffer。在实施时固定,因此只读。TypedArray.prototype.byteOffset只读返回类型数组从其开始的偏移量(以字节为单位)ArrayBuffer。在实施时固定,因此只读。TypedArray.prototype.length只读返回类型数组中保存的元素数。在实施时固定,因此只读。

方法

TypedArray.prototype.copyWithin()复制数组中的一系列数组元素。另见Array.prototype.copyWithin()TypedArray.prototype.entries()返回Array Iterator包含数组中每个索引的键/值对的新对象。另见Array.prototype.entries()TypedArray.prototype.every()测试数组中的所有元素是否都通过了函数提供的测试。另见Array.prototype.every()TypedArray.prototype.fill()使用静态值从开始索引到结束索引填充数组的所有元素。另见Array.prototype.fill()TypedArray.prototype.filter()用提供的过滤函数返回true的此数组的所有元素创建一个新数组。另见Array.prototype.filter()TypedArray.prototype.find()如果数组中的元素满足提供的测试函数或者undefined未找到,则返回数组中找到的值。另见Array.prototype.find()TypedArray.prototype.findIndex()如果数组中的元素满足提供的测试函数,则返回数组中找到的索引;如果未找到,则返回-1。另见Array.prototype.findIndex()TypedArray.prototype.forEach()为数组中的每个元素调用一个函数。另见Array.prototype.forEach()TypedArray.prototype.includes()确定类型化数组是否包含某个元素,返回true还是false适当的。另见Array.prototype.includes()TypedArray.prototype.indexOf()返回数组中等于指定值的元素的第一个(最少)索引,如果没有找到,则返回-1。另见Array.prototype.indexOf()TypedArray.prototype.join()将数组的所有元素连接到一个字符串中。另见Array.prototype.join()TypedArray.prototype.keys()返回Array Iterator包含数组中每个索引的键的新键。另见Array.prototype.keys()TypedArray.prototype.lastIndexOf()返回数组中等于指定值的元素的最后(最大)索引,如果没有找到,则返回-1。另见Array.prototype.lastIndexOf()TypedArray.prototype.map()使用在此数组中的每个元素上调用提供的函数的结果创建一个新数组。另见Array.prototype.map()TypedArray.prototype.move()UnimplementedFormer的非标准版本TypedArray.prototype.copyWithin()TypedArray.prototype.reduce()针对累加器和阵列的每个值(从左到右)应用函数,以将其减少为单个值。另见Array.prototype.reduce()TypedArray.prototype.reduceRight()针对累加器和阵列的每个值(从右到左)应用函数,以将其减少为单个值。另见Array.prototype.reduceRight()TypedArray.prototype.reverse()颠倒数组元素的顺序 - 第一个变为最后一个,最后一个变为第一个。另见Array.prototype.reverse()TypedArray.prototype.set()在类型数组中存储多个值,从指定数组中读取输入值。TypedArray.prototype.slice()提取数组的一部分并返回一个新数组。另见Array.prototype.slice()TypedArray.prototype.some()如果此数组中至少有一个元素满足提供的测试函数,则返回true。另见Array.prototype.some()TypedArray.prototype.sort()对数组中的元素进行排序并返回数组。另见Array.prototype.sort()TypedArray.prototype.subarray()从给定的开始和结束元素索引中返回一个新的TypedArray。TypedArray.prototype.values()返回Array Iterator包含数组中每个索引值的新对象。另见Array.prototype.values()TypedArray.prototype.toLocaleString()返回表示数组及其元素的本地化字符串。另见Array.prototype.toLocaleString()TypedArray.prototype.toString()返回表示数组及其元素的字符串。另见Array.prototype.toString()TypedArray.prototype[@@iterator]()返回一个新的Array Iterator 包含数组中每个索引值的对象。

Polyfill方法

Typed Arrays中使用的许多方法可以使用常规Javascript数组中的方法进行多边填充。下面的Javascript代码演示了如何使用它来自动填充所有在用户当前使用的浏览器中不支持的类型化数组方法。

var typedArrayTypes = [Int8Array, Uint8Array, Uint8ClampedArray, Int16Array,
          Uint16Array, ​​​Int32Array, Uint32Array, ​​​Float32Array, Float64Array];

for (var k in typedArrayTypes)
    for (var v in Array.prototype)
        if (Array.prototype.hasOwnProperty(v) && 
          !typedArrayTypes[k].prototype.hasOwnProperty(v))
            typedArrayTypes[k].prototype[v] = Array.prototype[v];

规格

Specification

Status

Comment

Typed Array Specification

Obsolete

Defined as TypedArray and ArrayBufferView interface with typed array view types. Superseded by ECMAScript 2015.

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'TypedArray Objects' in that specification.

Standard

Initial definition in an ECMA standard. Specified behaviour for indexed and named properties. Specified that new is required.

ECMAScript Latest Draft (ECMA-262)The definition of 'TypedArray Objects' in that specification.

Living Standard

ECMAScript 2017 changed the TypedArray 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

Indexed properties not consulting prototype

(Yes) 1

?

25 (25)

?

?

?

Named properties

(Yes)

?

30 (30)

?

?

?

new is required

?

?

44 (44)

?

?

?

Iterable in constructor

?

?

52 (52)

?

?

?

Constructor without arguments

?

?

55 (55)

?

?

?

Feature

Android

Android Webview

Edge

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Chrome for Android

Basic support

4.0

(Yes)

(Yes)

4.0 (2)

10

11.6

4.2

(Yes)

Indexed properties not consulting prototype

?

(Yes) 1

?

25.0 (25)

?

?

?

(Yes) 1

Named properties

?

(Yes)

?

30.0 (30)

?

?

?

?

new is required

?

?

?

44.0 (44)

?

?

?

?

Iterable in constructor

?

?

?

52.0 (52)

?

?

?

?

Constructor without arguments

?

?

?

55.0 (55)

?

?

?

?

1 -1和相似值不被视为索引属性,因此会返回原型属性的值。

兼容性说明

从ECMAScript 2015开始,TypedArray构造函数需要由new操作员构建。将TypedArray构造函数作为一个函数调用new,将会从现在开始抛出一个TypeError

var dv = Int8Array([1, 2, 3]);
// TypeError: calling a builtin Int8Array constructor 
// without new is forbidden
var dv = new Int8Array([1, 2, 3]);
TypedArray
typedArray.@@iterator 详细
TypedArray.@@species 详细
typedArray.buffer 详细
typedArray.byteLength 详细
typedArray.byteOffset 详细
TypedArray.BYTES_PER_ELEMENT 详细
typedArray.copyWithin 详细
typedArray.entries 详细
typedArray.every 详细
typedArray.fill 详细
typedArray.filter 详细
typedArray.find 详细
typedArray.findIndex 详细
typedArray.forEach 详细
TypedArray.from 详细
typedArray.includes 详细
typedArray.indexOf 详细
typedArray.join 详细
typedArray.keys 详细
typedArray.lastIndexOf 详细
typedArray.length 详细
typedArray.map 详细
TypedArray.name 详细
TypedArray.of 详细
TypedArray.prototype 详细
typedArray.reduce 详细
typedArray.reduceRight 详细
typedArray.reverse 详细
typedArray.set 详细
typedArray.slice 详细
typedArray.some 详细
typedArray.sort 详细
typedArray.subarray 详细
typedArray.toLocaleString 详细
typedArray.toString 详细
typedArray.values 详细
JavaScript

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络