javascript es6 class call parent method code example
Example 1: extended class call method from super in javascript
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hi, all my name is ${this.name}`);
}
}
class Employee extends Person {
constructor(name, age, employer) {
super(name, age);
this.employer = employer;
}
greeting() {
super.greet();
console.log(`I'm working at ${this.employer}`);
}
}
let Steve = new Employee('Xman', 25 , 'Skynet');
Steve;
Steve.greeting();
Example 2: setting property to method in child class javascript
class Person{
constructor(name,place){
this.name = name;
this.place = place;
}
Full(){
console.log(`I am ${this.name} from ${this.place}`);
}
};
class Student extends Person{
constructor(name,place,age){
super(name,place);
this.age = age;
}
Detail(){
this.area = "Adayar";
console.log(`I'm ${this.name} from ${this.place}, area ${this.area} & I'm ${this.age} years old`);
}
Detailed(){
console.log(`${this.area}`);
}
};
var student1 = new Student("Surya", "Chenai", 25);
student1;
student1.Full();
student1.Detail();
student1.Detailed();