I try to call parent method but I get error:
Error: call super outside of class constructor
My example :
class xo{
cool(x){
console.log(`parent init${x}`)
}
}
class boo extends xo{
cool(val){
super(val);
console.log(`child init${x}`)
}
}
x = new boo;
I try to call parent method but I get error:
Error: call super outside of class constructor
My example :
class xo{
cool(x){
console.log(`parent init${x}`)
}
}
class boo extends xo{
cool(val){
super(val);
console.log(`child init${x}`)
}
}
x = new boo;
Share
Improve this question
asked Sep 15, 2017 at 7:31
zloctbzloctb
11.2k10 gold badges76 silver badges91 bronze badges
2 Answers
Reset to default 17You call not the parent method, but parent constructor which is not valid call outside of the constructor. You need to use super.cool(val);
instead of super(val)
;
class xo{
cool(x) {
console.log(`parent init${x}`)
}
}
class boo extends xo {
cool(val) {
super.cool(val);
console.log(`child init${x}`)
}
}
x = new boo();
Use super.cool(val)
instead to call the cool
method on the super class. super()
invokes the super class' constructor.