Promise[Symbol.species]
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Promise[Symbol.species]
静态访问器属性返回用于从 Promise 方法构造返回值的构造函数。
¥The Promise[Symbol.species]
static accessor property returns the constructor used to construct return values from promise methods.
警告:
[Symbol.species]
的存在允许执行任意代码并可能产生安全漏洞。它还使某些优化变得更加困难。引擎实现者是 研究是否删除此功能。如果可能的话,避免依赖它。¥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.
语法
返回值
描述
¥Description
[Symbol.species]
访问器属性返回 Promise
对象的默认构造函数。子类构造函数可以重写它以更改构造函数赋值。默认实现基本上是:
¥The [Symbol.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:
// Hypothetical underlying implementation for illustration
class Promise {
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.
class SubPromise extends Promise {}
SubPromise[Symbol.species] === SubPromise; // true
Promise 链接方法 — then()
、catch()
和 finally()
— 返回新的 Promise 对象。他们通过 this.constructor[Symbol.species]
获取构造函数来构造新的 Promise。如果 this.constructor
是 undefined
,或者 this.constructor[Symbol.species]
是 undefined
或 null
,则使用默认的 Promise()
构造函数。否则,使用 this.constructor[Symbol.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[Symbol.species]
. If this.constructor
is undefined
, or if this.constructor[Symbol.species]
is undefined
or null
, the default Promise()
constructor is used. Otherwise, the constructor returned by this.constructor[Symbol.species]
is used to construct the new promise object.
示例
普通对象中的物种
派生对象中的物种
¥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.
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.
class MyPromise extends Promise {
someValue = 1;
}
console.log(MyPromise.resolve(1).then(() => {}).someValue); // 1
通过重写 [Symbol.species]
,promise 方法将返回基本 Promise
类型。
¥By overriding [Symbol.species]
, the promise methods will return the base Promise
type.
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 |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also