我试图使用mixins来理解ES6多重继承。
我关注this article(简单的mixins)。但是当我运行代码时,我的输出是:
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();
关于我做错的任何想法?
谢谢!
答案 0 :(得分:8)
如果您的超类包含构造函数,则必须在子构造函数中调用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();