I recently saw a similar question while looking at technical interview questions and it got me thinking about object inheritance more generally in JavaScript.
Here is some simple code:
class Car {
constructor(make,model) {
this.make = make
this.model = model
}
emitsCarbon() {
return true
}
}
class Hybrid extends Car {
constructor(...args) {
super(...args)
}
emitsCarbon() {
return false
}
}
let car1 = new Hybrid('toyota', 'prius')
car1.emitsCarbon() // returns false
What I am curious about is if there is a way to call the original emitsCarbon()
method that returns true (from the parent class) in the instance of child class?
My assumption would be no, once it is overridden, I would have to override the method again in the instance of car1 to get it to return true when the function is called.
The lack of knowledge I am seeking to fill is: in JavaScript is it possible to refer back up level in class inheritance and see all the parent class properties (methods, etc.) given an instance of a child class. It looks like this is possible in other languages (e.g., java), however, I do not have as much experience with those languages as much.
Lastly, whether this is useful, i.e., whether one should be more careful about overriding properties that should not be overridden, I am less certain. I think that one shouldn't. However, it is useful to better understand moving up a level in classes and class inheritance hierarchy in general.
This is not a duplicate of: How to call a parent method from child class in javascript?
I understand the calling of a parent method in a child class, however, I do not understand the calling (if at all possible, which it seems not) of a parent method in a child instance.