Reputation: 20406
I have a class BMW that extends the class Car and override a method printModel:
class Car{
constructor(model){
this.model = model
}
printModel(){
console.log(this.model)
}
}
class BMW extends Car{
constructor(model, color){
super(model);
this.color = color
}
printModel(){
console.log(this.model, this.color)
}
}
let bmw = new BMW('F6', 'blue')
bmw.printModel() //will print : 'F6 blue'
bmw.super.printModel()// expected: 'F6' but not working
How to call a class super method on an instance of this BMW class?
Upvotes: 0
Views: 1968
Reputation: 370689
You'll need a way to eventually get to super
when calling something on the instantiation. One option is to define another method on BMW
that calls super.printModel
:
class Car {
constructor(model) {
this.model = model
}
printModel() {
console.log(this.model)
}
}
class BMW extends Car {
constructor(model, color) {
super(model);
this.color = color
}
printModelBMW() {
console.log(this.model, this.color)
}
printModelCar() {
super.printModel();
}
}
let bmw = new BMW('F6', 'blue')
bmw.printModelBMW() //will print : 'F6 blue'
bmw.printModelCar() // expected: 'F6'
Upvotes: 2
Reputation: 222369
If parent class method needs to be used alongside with child class method, this means class design went wrong, overriding parent method was a mistake.
It could be:
class Car{
...
printGenericModel(){
console.log(this.model)
}
}
class BMW extends Car{
...
printModel(){
console.log(this.model, this.color)
}
}
Or in case parent class can't be refactored:
class Car{
...
printModel(){
console.log(this.model)
}
}
class BMW extends Car{
...
get printGenericModel(){
return super.printModel;
}
printModel(){
console.log(this.model, this.color)
}
}
Both methods are available as printGenericModel
and printModel
on BMW
instance.
Upvotes: -1
Reputation: 12419
The super
keyword can only be used inside a class, not outside. It's technically possible to call Car
's printModel
method directly, but this should really be avoided. The whole point of overriding a method in a superclass (like printModel
) is to give it an implementation more appropriate to the subclass. So circumventing this is an indication of improper OO design.
However, for educational purposes I figure it would still be useful to know how this can technically be done, since it reveals how JS classes are really still using prototypes under the hood:
Car.prototype.printModel.call(bmw)
One way to improve the design to avoid this would be to add an optional argument to specify how you want to print the model:
class BMW extends Car{
constructor(model, color){
super(model)
this.color = color
}
printModel(includeColor){
if (includeColor) {
console.log(this.model, this.color)
}
else super.printModel()
}
}
Upvotes: 1
Reputation: 3040
class Car{
constructor(model){
this.model = model
}
printModel(){
console.log(this.model)
}
getModel()
{
return this.model
}
}
class BMW extends Car{
constructor(model, color){
super(model);
this.color = color
}
printModel(){
console.log(super.getModel(), this.color)
}
}
Upvotes: 0
Reputation: 6063
It's not possible to reference the super instance outside of the context of the class. If you really must use the method from the super instance from outside of the class, you can call the method yourself:
Car.prototype.printModel.call(bmw);
Upvotes: 2