TypedArray.from()

TypedArray.from() 静态方法从类似数组或可迭代对象创建新的 类型数组。该方法与 Array.from() 几乎相同。

¥The TypedArray.from() static method creates a new typed array from an array-like or iterable object. This method is nearly the same as Array.from().

Try it

语法

¥Syntax

js
TypedArray.from(arrayLike, mapFn)
TypedArray.from(arrayLike, mapFn, thisArg)

其中 TypedArray 是以下之一:

¥Where TypedArray is one of:

参数

¥Parameters

arrayLike

要转换为类型化数组的可迭代或类似数组的对象。

mapFn Optional

调用类型化数组的每个元素的函数。如果提供,则首先将要添加到数组的每个值都通过此函数传递,然后将 mapFn 的返回值添加到类型化数组中。使用以下参数调用该函数:

element

类型化数组中正在处理的当前元素。

index

类型化数组中当前正在处理的元素的索引。

thisArg Optional

执行 mapFn 时用作 this 的值。

返回值

¥Return value

一个新的 TypedArray 实例。

¥A new TypedArray instance.

描述

¥Description

详细信息请参见 Array.from()

¥See Array.from() for more details.

Array.from()TypedArray.from() 之间有一些细微的区别(注意:下面提到的 this 值是调用 TypedArray.from() 时使用的 this 值,而不是用于调用 mapFnthisArg 参数):

¥There are some subtle distinctions between Array.from() and TypedArray.from() (note: the this value mentioned below is the this value that TypedArray.from() was called with, not the thisArg argument used to invoke mapFn):

  • 如果 TypedArray.from()this 值不是构造函数,则 TypedArray.from() 将抛出 TypeError,而 Array.from() 默认创建新的 Array
  • this 构造的对象必须是 TypedArray 实例,而 Array.from() 允许其 this 值构造为任何对象。
  • source 参数是迭代器时,TypedArray.from() 首先从迭代器收集所有值,然后使用计数创建 this 的实例,最后在实例上设置值。Array.from() 在从迭代器接收到每个值时设置它们,然后在最后设置它的 length
  • TypedArray.from() 使用 [[Set]],而 Array.from() 使用 [[DefineOwnProperty]]。因此,当使用 Proxy 对象时,它会调用 handler.set() 来创建新元素,而不是 handler.defineProperty()
  • Array.from() 获得一个不是迭代器的类数组时,它会考虑漏洞。TypedArray.from() 将确保结果是密集的。

示例

¥Examples

来自可迭代对象(Set)

¥From an iterable object (Set)

js
const s = new Set([1, 2, 3]);
Uint8Array.from(s);
// Uint8Array [ 1, 2, 3 ]

来自字符串

¥From a string

js
Int16Array.from("123");
// Int16Array [ 1, 2, 3 ]

与箭头函数和映射一起使用

¥Use with arrow function and map

使用箭头函数作为映射函数来操作元素

¥Using an arrow function as the map function to manipulate the elements

js
Float32Array.from([1, 2, 3], (x) => x + x);
// Float32Array [ 2, 4, 6 ]

生成数字序列

¥Generate a sequence of numbers

js
Uint8Array.from({ length: 5 }, (v, k) => k);
// Uint8Array [ 0, 1, 2, 3, 4 ]

在非 TypedArray 构造函数上调用 from()

¥Calling from() on non-TypedArray constructors

from()this 值必须是返回 TypedArray 实例的构造函数。

¥The this value of from() must be a constructor that returns a TypedArray instance.

js
function NotArray(len) {
  console.log("NotArray called with length", len);
}

Int8Array.from.call({}, []); // TypeError: #<Object> is not a constructor
Int8Array.from.call(NotArray, []);
// NotArray called with length 0
// TypeError: Method %TypedArray%.from called on incompatible receiver #<NotArray>
js
function NotArray2(len) {
  console.log("NotArray2 called with length", len);
  return new Uint8Array(len);
}
console.log(Int8Array.from.call(NotArray2, [1, 2, 3]));
// NotArray2 called with length 3
// Uint8Array(3) [ 1, 2, 3 ]

规范

Specification
ECMAScript Language Specification
# sec-%typedarray%.from

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看