class Person{
contrustor(name){
this.name = name;
}
get name(){
return this._name;
}
set name(){
if(value)
this._name = value;
}
}
}
classEmployeeextendsPerson{doWork(){return`${this._name} is working`; }}let p1 =newPerson ("John");let e1 =newEmployee ("Jane");p1.name; //returns "John"e1.name; //returns "Jane" e1.doWork() ///returns "Jane is Working"
super keyword
super key is used to call/invoke the parent's constructor.
class Person{
contrustor(name){
this.name = name;
}
get name(){
return this._name;
}
set name(){
if(value)
this._name = value;
}
}
}
classEmployeeextendsPerson{constructor(title, name){super(name);this._title = title; }gettitle(){returnthis._title; }doWork(){//super(); //it can be used in other methods alsoreturn`${this._name} is working`; }}let e1 =newEmployee ("Developer","John");e1.name; //returns "John" e1.title; //returns "Developer"e1.doWork() ///returns "John is Working"
classEmployeeextendsPerson{constructor(title, name){super(name);this._title = title; }gettitle(){returnthis._title; }doWork(){returnsuper() +"paid"//can also invoke doWork method of the parent class. //if this keyword is used it will invoke the method of the same class i.e Employee and it will give StackOverflow error.
}}let e1 =newEmployee ("Developer","John");let p1 =newPerson ("Alex");p1.doWork()e1.doWork()