I am trying to understand why this
can be accessed inside both a constructor function and its prototype method. I know this
will implicitly become an object when a constructor function is invoked with new
, but I don't understand why this
can be accessed in the prototype method as following:
function Person(name) {
this.name = name;
}
Person.prototype.hi = function () {
console.log(`Hi! My name is ${this.name}.`);
};
const john = new Person('john')
john.hi() //Hi! My name is john.
Is the this
saved somewhere in global to that instance and its methods?