TypedArray.of()

TypedArray.of() 静态方法从可变数量的参数创建一个新的 类型数组。该方法与 Array.of() 几乎相同。

¥The TypedArray.of() static method creates a new typed array from a variable number of arguments. This method is nearly the same as Array.of().

Try it

语法

¥Syntax

js
TypedArray.of()
TypedArray.of(element1)
TypedArray.of(element1, element2)
TypedArray.of(element1, element2, /* …, */ elementN)

其中 TypedArray 是以下之一:

¥Where TypedArray is one of:

参数

¥Parameters

element1, …, elementN

用于创建类型化数组的元素。

返回值

¥Return value

一个新的 TypedArray 实例。

¥A new TypedArray instance.

描述

¥Description

详细信息请参见 Array.of()Array.of()TypedArray.of() 之间有一些细微的区别:

¥See Array.of() for more details. There are some subtle distinctions between Array.of() and TypedArray.of():

  • 如果传递给 TypedArray.of()this 值不是构造函数,则 TypedArray.from() 将抛出 TypeError,而 Array.of() 默认创建新的 Array
  • TypedArray.of() 使用 [[Set]],而 Array.of() 使用 [[DefineOwnProperty]]。因此,当使用 Proxy 对象时,它会调用 handler.set() 来创建新元素,而不是 handler.defineProperty()

示例

¥Examples

使用()

¥Using of()

js
Uint8Array.of(1); // Uint8Array [ 1 ]
Int8Array.of("1", "2", "3"); // Int8Array [ 1, 2, 3 ]
Float32Array.of(1, 2, 3); // Float32Array [ 1, 2, 3 ]
Int16Array.of(undefined); // Int16Array [ 0 ]

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看