JS TypedArray

🌙
手机阅读
本文目录结构

一个 TypedArray 对象描述一个底层的二进制数据缓存区的一个类似数组 (array-like) 视图。事实上,没有名为 TypedArray 的全局对象,也没有一个名为的 TypedArray 构造函数。相反,有许多不同的全局对象,下面会列出这些针对特定元素类型的类型化数组的构造函数。在下面的页面中,你会找到一些不管什么类型都公用的属性和方法。

// create a TypedArray with a size in bytes
const typedArray1 = new Int8Array(8);
typedArray1[0] = 32;

const typedArray2 = new Int8Array(typedArray1);
typedArray2[1] = 42;

console.log(typedArray1);
// expected output: Int8Array [32, 0, 0, 0, 0, 0, 0, 0]

console.log(typedArray2);
// expected output: Int8Array [32, 42, 0, 0, 0, 0, 0, 0]

语法

new TypedArray(); // ES2017中新增
new TypedArray(length);
new TypedArray(typedArray);
new TypedArray(object);
new TypedArray(buffer [, byteOffset [, length]]);

TypedArray()指的是以下的其中之一:

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

参数

length

当传入 length 参数时,一个内部数组缓冲区会被创建在内存中。该缓存区的大小是传入的 length 乘以数组中每个元素的字节数(BYTES_PER_ELEMENT),每个元素的值都为 0。(译者注:每个元素的字节数是由具体的构造函数决定的,比如 Int16Array 的每个元素的字节数为 2,Int32Array 的每个元素的字节数为 4)

typedArray

当传入一个包含任意类型元素的任意类型化数组对象 typedArray(比如 Int32Array)作为参数时,typedArray 被复制到一个新的类型数组。typedArray 中的每个值会在复制到新的数组之前根据构造器进行转化。新的生成的类型化数组对象将会有跟传入的数组相同的长度(译者注:比如原来的 typedArray.length==2,那么新生成的数组的 length 也是 2,只是数组中的每一项进行了转化)

object

当传入一个 object 作为参数时,如同通过 TypedArray.from() 方法一样创建一个新的类型数组。

buffer, byteOffset, length

当传入一个 buffer 参数,或者再另外加上可选参数 byteOffset 和 length 时,一个新的类型化数组视图将会被创建并可用于呈现传入的 ArrayBuffer 实例。byteOffset 和 length 指定类型化数组视图暴露的内存范围,如果两者都未传入,那么整个 buffer 都会被呈现。如果仅仅忽略 length,那么 buffer 中偏移 byteOffset 后剩下的 buffer 将会被呈现。

描述

ECMAScript 6 定义 TypeArray 构造器作为所有的类型化数组构造器 (Int8Array,Int16Array 等)的原型。该构造器不会直接暴露:没有全局的 %TypedArray% 和 TypeArray 属性。只能通过使用类似 Object.getPrototypeOf(Int8Array.prototype) 的方式进行访问。所有的类型化数组构造器 (Int8Array,Int16Array 等)都会继承 TypeArray 构造器的通用属性和方法。此外,所有的类型化数组原型 (Int8Array.prototype,Int16Array.prototype 等)的原型都以 TypeArray.prototype 作为原型。

TypedArray 构造器自身不是特别有用。调用或在一个表达式中使用它都会抛出一个 TypeError 异常,除非在支持通过继承创建对象的 JS 引擎下运行。但直到现在还没有这样的 JS 引擎出现,因此 TypeArray 仅仅是对所有的类型化类构造器 (Int8Array,Int16Array 等)的方法和属性进行 polyfill 的时候比较有用。

当创建一个 TypedArray 实例(例如:Int8Array)时,一个数组缓冲区将被创建在内存中,如果 ArrayBuffer 对象被当作参数传给构造函数将使用传入的 ArrayBuffer 代替。缓冲区的地址被存储在实例的内部属性中,所有的 %TypedArray%.prototype 上的方法例如 set value 和 get value 等都会操作在数组缓冲区上。

属性访问

你可以参考使用标准数组索引数组中的元素的方法(其实就是方括号里面写下标). 然而,原型链上面定义的索引属性(译者注:即用数字作为属性,例如Int16Array.prototype[0]=12;),

在实例化的对象上面是获取不到该属性的(int16Array[0]==undefined). 通过查询 ArrayBuffer 是找不到索引属性的。但您仍然可以使用命名属性(译者注:就是键不是数字的), 就像所有对象一样。

// 设置和使用标准数组语法
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 对象

类型 大小(字节单位) 描述 Web IDL type C 语言中的等效类型
Int8Array 1 8 位二进制带符号整数 -2^7~(2^7) - 1 byte int8_t
Uint8Array 1 8 位无符号整数 0~(2^8) - 1 octet uint8_t
Int16Array 2 16 位二进制带符号整数 -2^15~(2^15)-1 short int16_t
Uint16Array 2 16 位无符号整数 0~(2^16) - 1 unsigned short uint16_t
Int32Array 4 32 位二进制带符号整数 -2^31~(2^31)-1 long int32_t
Uint32Array 4 32 位无符号整数 0~(2^32) - 1 unsigned int uint32_t
Float32Array 4 32 位 IEEE 浮点数 unrestricted float float
Float64Array 8 64 位 IEEE 浮点数 unrestricted double double

属性

TypedArray.BYTES_PER_ELEMENT

返回不同类型的数组对象的元素大小的数字值。

TypedArray.length

Length property whose value is 3.(译者注:应该是数组的长度吧???)

TypedArray.name

返回构造器的名称,例如"Int8Array".

get TypedArray[@@species]

用于创建派生对象的构造函数函数。

TypedArray.prototype

TypedArray 的原型。

方法

TypedArray.from()

使用类数组 (array-like) 或迭代对象创建一个新的类型化数组。参见 Array.from().

TypedArray.of()

通过可变数量的参数创建新的类型化数组。参见 Array.of().

TypedArray 原型

所有的类型化数组都是继承自 TypedArray.prototype.

属性

TypedArray.prototype.constructor

返回创建实例原型的构造函数。这是相应的 typed array type 的默认的构造函数。

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()

使用指定的函数测试所有元素,并创建一个包含所有通过测试的元素的新数组。参见 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.prototype.keys().

TypedArray.prototype.lastIndexOf()

返回数组中最后一个等于指定值得元素的索引,如果找不到则返回 -1. 参见 Array.prototype.lastIndexOf().

TypedArray.prototype.map()

创建一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组。参见 Array.prototype.map().

TypedArray.prototype.move() 未实现

以前的不标准版本的 TypedArray.prototype.copyWithin().

TypedArray.prototype.reduce()

接收一个函数作为累加器(accumulator), 数组中的每个值(从左到右)开始合并,最终为一个值。参见 Array.prototype.reduce().

TypedArray.prototype.reduceRight()

接受一个函数作为累加器(accumulator), 让每个值(从右到左,亦即从尾到头)缩减为一个值。(与 reduce() 的执行方向相反). 参见 Array.prototype.reduceRight().

TypedArray.prototype.reverse()

颠倒数组中元素的位置。第一个元素会成为最后一个,最后一个会成为第一个。参见 Array.prototype.reverse().

TypedArray.prototype.set()

读取一个指定数组中的元素保存到格式化数组中。

TypedArray.prototype.slice()

浅复制(shallow copy)数组的一部分到一个新的数组,并返回这个新数组。参见 Array.prototype.slice().

TypedArray.prototype.some()

数组中只要有一个元素满足提供的测试函数的测试就返回 true, 否则返回 false. 参见 Array.prototype.some().

TypedArray.prototype.sort()

对数组进行排序,并返回原数组(是改变原数组). 参见 Array.prototype.sort().

TypedArray.prototype.subarray()

返回给定的起始和结束索引之间的元素组成的新的类型化数组。

TypedArray.prototype.values()

返回有数组中的元素组成的新的数组迭代对象。参见 Array.prototype.values().

TypedArray.prototype.toLocaleString()

返回一个将数组中的每个元素本地化后组成的字符串。参见 Array.prototype.toLocaleString().

TypedArray.prototype.toString()

返回一个由数组中的每个元素字符串化后组成的字符串。参见 Array.prototype.toString().

TypedArray.prototype@@iterator

返回一个包含数组中每个元素的新的数组迭代对象。

AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

于2021年离开前端领域,目前重心放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>

于2021年离开前端领域,目前从事区块链方面工作了