We also learned how to inherit objects using the old method, but with the ES6 update, inheritance is much easier and simpler!
Steps to Inherit the Classes :
First, let us create our parent class.
class Parent{
constructor(name, age) {
this.name = name;
this.age = age;
}
}
JavaScript
Now letβs create our child class and inherit it with the parent class
//while inheriting the classes we need to use extend word, which basically extends the class further
class Child extends Parent{
constructor(name, age){
//super is a function that uses the parent class's constructor!
super(name, age);
}
sentence(){
console.log(`Your name is ${this.name} and You're ${this.age} years old`;)
}
}
JavaScript
Dry run
const child = new Child('meer', 19);
console.log(child.sentence())
//output -> Your name is meer and You're 19 years old
JavaScript
For reference :
