类型化数组[@@物种]

TypedArray[@@species] 静态访问器属性返回用于构造类型化数组方法的返回值的构造函数。

¥The TypedArray[@@species] static accessor property returns the constructor used to construct return values from typed array methods.

警告:@@species 的存在允许执行任意代码并可能产生安全漏洞。它还使某些优化变得更加困难。引擎实现者是 研究是否删除此功能。如果可能的话,避免依赖它。

¥Warning: The existence of @@species allows execution of arbitrary code and may create security vulnerabilities. It also makes certain optimizations much harder. Engine implementers are investigating whether to remove this feature. Avoid relying on it if possible.

语法

¥Syntax

js
TypedArray[Symbol.species]

返回值

¥Return value

调用 get @@species 的构造函数 (this) 的值。返回值用于从创建新类型化数组的类型化数组方法构造返回值。

¥The value of the constructor (this) on which get @@species was called. The return value is used to construct return values from typed array methods that create new typed arrays.

描述

¥Description

@@species 访问器属性返回 类型数组 对象的默认构造函数。子类构造函数可以重写它以更改构造函数赋值。默认实现基本上是:

¥The @@species accessor property returns the default constructor for typed array objects. Subclass constructors may override it to change the constructor assignment. The default implementation is basically:

js
// Hypothetical underlying implementation for illustration
class TypedArray {
  static get [Symbol.species]() {
    return this;
  }
}

由于这种多态实现,派生子类的 @@species 也会默认返回构造函数本身。

¥Because of this polymorphic implementation, @@species of derived subclasses would also return the constructor itself by default.

js
class SubTypedArray extends Int8Array {}
SubTypedArray[Symbol.species] === SubTypedArray; // true

当调用不改变现有数组但返回新数组实例(例如 filter()map())的类型化数组方法时,将访问该数组的 constructor[@@species]。返回的构造函数将用于构造类型化数组方法的返回值。

¥When calling typed array methods that do not mutate the existing array but return a new array instance (for example, filter() and map()), the array's constructor[@@species] will be accessed. The returned constructor will be used to construct the return value of the typed array method.

然而,与 Array[@@species] 不同,当使用 @@species 创建新的类型化数组时,该语言将确保新创建的数组是正确的类型化数组,并且具有与原始数组相同的内容类型 - 例如,你不能创建 BigInt64ArrayFloat64Array 创建一个非 BigInt 数组,或者从 BigInt 数组创建一个非 BigInt 数组。这样做会抛出 TypeError

¥However, unlike Array[@@species], when using @@species to create new typed arrays, the language will make sure that the newly created array is a proper typed array and has the same content type as the original array — for example, you can't create a BigInt64Array from a Float64Array, or create a non-BigInt array from a BigInt array. Doing so throws a TypeError.

js
class BadArray extends Int8Array {
  static get [Symbol.species]() {
    return Array;
  }
}
new BadArray(1).map(() => 0); // TypeError: Method %TypedArray%.prototype.map called on incompatible receiver [object Array]

class BadArray2 extends Int8Array {
  static get [Symbol.species]() {
    return BigInt64Array;
  }
}
new BadArray2(1).map(() => 0n); // TypeError: TypedArray.prototype.map constructed typed array of different content type from |this|

注意:由于 SpiderMonkey 和 V8 中的错误,未检查内容类型匹配。在第二个示例中,只有 Safari 会抛出 TypeError

¥Note: Due to a bug in both SpiderMonkey and V8, the content type match is not checked. Only Safari will throw a TypeError in the second example.

示例

¥Examples

普通对象中的物种

¥Species in ordinary objects

@@species 属性返回默认构造函数,它是任何给定 类型数组 构造函数的类型化数组构造函数本身之一。

¥The @@species property returns the default constructor function, which is one of the typed array constructors itself for any given typed array constructor.

js
Int8Array[Symbol.species]; // function Int8Array()
Uint8Array[Symbol.species]; // function Uint8Array()
Float32Array[Symbol.species]; // function Float32Array()

派生对象中的物种

¥Species in derived objects

在自定义 TypedArray 子类(例如 MyTypedArray)的实例中,MyTypedArray 种类是 MyTypedArray 构造函数。但是,你可能想要覆盖它,以便在派生类方法中返回父 类型数组 对象:

¥In an instance of a custom TypedArray subclass, such as MyTypedArray, the MyTypedArray species is the MyTypedArray constructor. However, you might want to overwrite this, in order to return a parent typed array object in your derived class methods:

js
class MyTypedArray extends Uint8Array {
  // Overwrite MyTypedArray species to the parent Uint8Array constructor
  static get [Symbol.species]() {
    return Uint8Array;
  }
}

规范

Specification
ECMAScript Language Specification
# sec-get-%typedarray%-@@species

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看