I would like to extend the following class:
class Person {
constructor(name, age) {
this._name = name;
this._age = age;
}
get name() {
return this._name;
}
get age() {
return this._age;
}
}
...by adding sport and its getter:
class Athlete extends Person {
constructor(name, age, sport) {
super();
this._name = name;
this._age = age;
this._sport = sport;
}
get sport() {
return this._sport;
}
}
While the above works, I would like to avoid repeating the arguments of the base-parent constructor. The following approach won't work:
class Athlete extends Person {
constructor(sport) {
super();
this._sport = sport;
}
get sport() {
return this._sport;
}
}
let athlete = new Athlete('Peter', 29, 'cricket');
console.log(athlete.name, athlete.age, athlete.sport); // name and age are not inherited, is returned 'undefined'
So, how can I add fields to the subClass without rewriting the ones of the base Class?