【发布时间】:2016-09-19 03:23:06
【问题描述】:
我正在阅读 You Don't Know JS: ES6 & Beyond,我在 Symbol.species 部分遇到了这个 sn-p。
class Cool {
// defer `@@species` to derived constructor
static get [Symbol.species]() { return this; }
again() {
return new this.constructor[Symbol.species]();
}
}
class Fun extends Cool {}
class Awesome extends Cool {
// force `@@species` to be parent constructor
static get [Symbol.species]() { return Cool; }
}
var a = new Fun(),
b = new Awesome(),
c = a.again(),
d = b.again();
c instanceof Fun; // true
d instanceof Awesome; // false
d instanceof Cool; // true
似乎函数 Symbol.species{ return Something } 应该总是返回一个构造函数。但是在第一次出现这个函数时:
static get [Symbol.species]() { return this; }
我很困惑,因为我一直认为这应该是一个对象而不是构造函数。
请您帮我澄清一下事实好吗?
关于return new this.constructor[Symbol.species]();,这里指的是什么?
【问题讨论】:
标签: javascript symbols