Search⌘ K
AI Features

Solution Review: "super" Keyword

Explore the use of the super keyword in JavaScript object-oriented programming. Understand why calling super in child class constructors is necessary to properly initialize this. This lesson reviews common errors and fixes through practical code examples and explanations for junior to mid-level interview preparation.

Question 1: Solution review #

In the previous lesson, you were given the following code:

Javascript (babel-node)
function Human() {
this.name = "Tommy"
this.sex = "Male"
}
class Child extends Human {
constructor(){
this.age=2;
}
}
var child1 = new Child()

For the code above, you had to answer the following question:

Explanation #

Let’s incorporate each option in our code and see if it’s correct.

Option A: name and sex should be passed as parameters to Human().

Let’s do that and see the result.

Javascript (babel-node)
function Human(name,sex) {
this.name = "Tommy"
this.sex = "Male"
}
class Child extends Human {
constructor(){
this.age=2;
}
}
var child1 = new Child()

The error is still there, so this is not the issue. This makes sense since we are setting definite values for name and sex, so we ...