Array[Symbol.species]

Array[Symbol.species] 静态访问器属性返回用于构造数组方法返回值的构造函数。

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

警告:[Symbol.species] 的存在允许执行任意代码并可能产生安全漏洞。它还使某些优化变得更加困难。引擎实现者是 研究是否删除此功能。如果可能的话,避免依赖它。现代数组方法(例如 toReversed())不使用 [Symbol.species],并且始终返回新的 Array 基类实例。

¥Warning: The existence of [Symbol.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. Modern array methods, such as toReversed(), do not use [Symbol.species] and always return a new Array base class instance.

语法

¥Syntax

js
Array[Symbol.species]

返回值

¥Return value

调用 get [Symbol.species] 的构造函数 (this) 的值。返回值用于从创建新数组的数组方法构造返回值。

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

描述

¥Description

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

¥The [Symbol.species] accessor property returns the default constructor for Array objects. Subclass constructors may override it to change the constructor assignment. The default implementation is basically:

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

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

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

js
class SubArray extends Array {}
SubArray[Symbol.species] === SubArray; // true

当调用不改变现有数组但返回新数组实例(例如 filter()map())的数组方法时,将访问该数组的 constructor[Symbol.species]。返回的构造函数将用于构造数组方法的返回值。这使得在技术上可以使数组方法返回与数组无关的对象。

¥When calling array methods that do not mutate the existing array but return a new array instance (for example, filter() and map()), the array's constructor[Symbol.species] will be accessed. The returned constructor will be used to construct the return value of the array method. This makes it technically possible to make array methods return objects unrelated to arrays.

js
class NotAnArray {
  constructor(length) {
    this.length = length;
  }
}

const arr = [0, 1, 2];
arr.constructor = { [Symbol.species]: NotAnArray };
arr.map((i) => i); // NotAnArray { '0': 0, '1': 1, '2': 2, length: 3 }
arr.filter((i) => i); // NotAnArray { '0': 1, '1': 2, length: 0 }
arr.concat([1, 2]); // NotAnArray { '0': 0, '1': 1, '2': 2, '3': 1, '4': 2, length: 5 }

示例

¥Examples

普通对象中的物种

¥Species in ordinary objects

[Symbol.species] 属性返回默认构造函数,即 ArrayArray 构造函数。

¥The [Symbol.species] property returns the default constructor function, which is the Array constructor for Array.

js
Array[Symbol.species]; // [Function: Array]

派生对象中的物种

¥Species in derived objects

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

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

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

规范

Specification
ECMAScript Language Specification
# sec-get-array-@@species

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看