ES6 Class 通过 mixins 多重继承
ES6 Class multiple inheritance through mixins
我正在尝试使用 mixin 来理解 ES6 多重继承。
我正在关注 this article(简单的混入)。但是当我 运行 代码时,我的输出是:
main
Uncaught ReferenceError: this is not defined
,
并且错误再次指向 console.log('main')
我正在 运行 将其安装在最新版本的 Chrome 上。这是我的代码:
const RaceDayService = superclass => class extends superclass {
constructor(){
console.log('service');
}
}
const RaceDayDB = superclass => class extends superclass {
constructor(){
console.log('db');
}
}
class RaceDayUI {
constructor(){
console.log('ui');
}
}
class RaceDay extends RaceDayDB(RaceDayService(RaceDayUI)){
constructor(options){
console.log('main');
}
}
const raceDay = new RaceDay();
对我做错了什么有什么想法吗?
谢谢!
如果您的超类包含构造函数,则必须在子类的构造函数中调用 super()
方法:
const RaceDayService = superclass => class extends superclass {
constructor(){
super();
console.log('service');
}
}
const RaceDayDB = superclass => class extends superclass {
constructor(){
super();
console.log('db');
}
}
class RaceDayUI {
constructor(){
console.log('ui');
}
}
class RaceDay extends RaceDayDB(RaceDayService(RaceDayUI)){
constructor(options){
super();
console.log('main');
}
}
const raceDay = new RaceDay();
我正在尝试使用 mixin 来理解 ES6 多重继承。
我正在关注 this article(简单的混入)。但是当我 运行 代码时,我的输出是:
main
Uncaught ReferenceError: this is not defined
,
并且错误再次指向console.log('main')
我正在 运行 将其安装在最新版本的 Chrome 上。这是我的代码:
const RaceDayService = superclass => class extends superclass {
constructor(){
console.log('service');
}
}
const RaceDayDB = superclass => class extends superclass {
constructor(){
console.log('db');
}
}
class RaceDayUI {
constructor(){
console.log('ui');
}
}
class RaceDay extends RaceDayDB(RaceDayService(RaceDayUI)){
constructor(options){
console.log('main');
}
}
const raceDay = new RaceDay();
对我做错了什么有什么想法吗?
谢谢!
如果您的超类包含构造函数,则必须在子类的构造函数中调用 super()
方法:
const RaceDayService = superclass => class extends superclass {
constructor(){
super();
console.log('service');
}
}
const RaceDayDB = superclass => class extends superclass {
constructor(){
super();
console.log('db');
}
}
class RaceDayUI {
constructor(){
console.log('ui');
}
}
class RaceDay extends RaceDayDB(RaceDayService(RaceDayUI)){
constructor(options){
super();
console.log('main');
}
}
const raceDay = new RaceDay();