so this is the code i wrote:
const PersonProto = {
calcAge() {
return 2022 - this.birthYear;
},
init(firstName, birthYear) {
this.firstName = firstName;
this.birthYear = birthYear;
},
};
const walter = Object.create(PersonProto);
const StudentProto = Object.create(PersonProto);
StudentProto.init = function (firstName, birthYear, course) {
PersonProto.init.call(this, firstName, birthYear);
this.course = course;
};
StudentProto.introduce = function () {
console.log(
`hi i am ${this.firstName} who is ${this.calcAge} years old and is studying ${this.course}`
);
};
const penny = Object.create(StudentProto);
penny.init('Penny', 2021, 'maths');
penny.calcAge();
penny.introduce();
and the problem is that for penny.introduce(), I get
hi i am Penny who is calcAge() {
return 2022 - this.birthYear;
} years old and is studying maths
in the console. However penny.calcAge() does work correctly. So why does it not work inside of the other function? I’m just starting to learn about inheritance and prototypes so it’s hard for me to figure this out by myself. Thanks in advance, any help is welcome!