承诺[@@物种]

Promise[@@species] 静态访问器属性返回用于从 Promise 方法构造返回值的构造函数。

¥The Promise[@@species] static accessor property returns the constructor used to construct return values from promise 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
Promise[Symbol.species]

返回值

¥Return value

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

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

描述

¥Description

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

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

js
// Hypothetical underlying implementation for illustration
class Promise {
  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 SubPromise extends Promise {}
SubPromise[Symbol.species] === SubPromise; // true

Promise 链接方法 — then()catch()finally() — 返回新的 Promise 对象。他们通过 this.constructor[@@species] 获取构造函数来构造新的 Promise。如果 this.constructorundefined,或者 this.constructor[@@species]undefinednull,则使用默认的 Promise() 构造函数。否则,使用 this.constructor[@@species] 返回的构造函数来构造新的 Promise 对象。

¥Promise chaining methods — then(), catch(), and finally() — return new promise objects. They get the constructor to construct the new promise through this.constructor[@@species]. If this.constructor is undefined, or if this.constructor[@@species] is undefined or null, the default Promise() constructor is used. Otherwise, the constructor returned by this.constructor[@@species] is used to construct the new promise object.

示例

¥Examples

普通对象中的物种

¥Species in ordinary objects

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

¥The Symbol.species property returns the default constructor function, which is the Promise constructor for Promise.

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

派生对象中的物种

¥Species in derived objects

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

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

js
class MyPromise extends Promise {
  // Override MyPromise species to the parent Promise constructor
  static get [Symbol.species]() {
    return Promise;
  }
}

默认情况下,promise 方法将返回带有子类类型的 Promise。

¥By default, promise methods would return promises with the type of the subclass.

js
class MyPromise extends Promise {
  someValue = 1;
}

console.log(MyPromise.resolve(1).then(() => {}).someValue); // 1

通过重写 @@species,promise 方法将返回基本 Promise 类型。

¥By overriding @@species, the promise methods will return the base Promise type.

js
class MyPromise extends Promise {
  someValue = 1;
  static get [Symbol.species]() {
    return Promise;
  }
}

console.log(MyPromise.resolve(1).then(() => {}).someValue); // undefined

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看