In the below code when I do console.log(teacher1.getDetails()); the property value shows undefined but when I do console.log(teacher1.personName) or console.log(teacher.mainSubject) it shows property value. What is happening here? What am I missing? Can somebody explain it please? And how to make it work?
let Person = function(personName, age) {
this.personName = personName;
this.age = age;
};
Person.prototype.getDetails = function() {
return `Person Name:${this.personName}. Age is ${this.age}.`;
};
//Child constructor function
let Teacher = function(personName, age, mainSubject) {
Person.call(this, personName, age);
this.mainSubject = mainSubject;
};
Teacher.prototype = Object.create(Person.prototype); //inheritance
Teacher.prototype.getDetails = function() {
return `${this.__proto__.getDetails()} Main subject is ${this.mainSubject}.`; //optionally invoke the parent method.
};
let teacher1 = new Teacher("Sakib", 35, "Physics");
console.log(teacher1.getDetails()); //invokes Teacher.getDetails() method (child's method).