I'm trying to understand ES6 multiple inheritance using mixins.
I was following this article (simple mixins). But when I run the code, my output is:
main
Uncaught ReferenceError: this is not defined
,console.log('main')
againI’m running it on the latest version of Chrome. Here is my code:
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();
Any ideas on what I’m doing incorrectly?
Thanks!
If your superclass contains a constructor, you have to call the super()
method in the child's constructor:
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With