Well, my approach here may seem a bit different from what you have already done but I think it can be more concise and explanatory for being able to do efficient code maintenance in a future.
First of all, I created a class Person_params for declaring all properties which I will pass directly to my Person Class.
After this I also declared self = this; at the beginning of my Class Person which actually acquires all object properties and saves them to the self.
By using self, you can refer to the global scope in a way that will work not only in a window context (self will resolve to window.self) but also in a worker context (self will then resolve to WorkerGlobalScope.self). This part is from the documentation https://developer.mozilla.org/en-US/docs/Web/API/Window/self
At the end, when I call console.log('${self.name} hides!'); at my Student Class, it actually inherits the properties of Person Class which I passed with the Person_params Class. In this case Person_params is basically used as a Class which serves as a constructor for the Person Class.
//Params for Person Class
class Person_params {
//constructor params to pass to the Person class
constructor() {
this.name;
this.age;
}
}
class Person {
self = this;
constructor(Person_params) {
self.name = Person_params.name;
self.age= Person_params.age;
this.run();
}
walk() {
console.log(`${self.name} walks!`);
}
run() {
console.log(`${self.name} runs!`);
}
}
class Student extends Person {
// self inherits from its parent class
hide() {
console.log(`${self.name} hides!`);
}
}
let personParams = new Person_params();
personParams.name = "Raul";
personParams.age = 27;
let person = new Person(personParams);
let student = new Student(personParams);
student.walk();
student.hide();
athleteis part of an instance, IOW: does not exist unless your create an instance ofPerson, so it's not possible to call something that does not exist yet.athleteis more or less just an object, so you should create a separate class for it (e.g.Athlete). If you want, you can then create another class called something likeStudentAthlete, which extends theAthleteclass. Therunmethod can then usesuper.run().let o = new Student(); o.walk();it will indeed log on console "parent call" as expected because the call on o.walk() will linvoke the function in the base class that logs that string on console. I still don't get what's wrong. Sorry I got it now.. I'm going to rephrase soon...