1

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?

John Winston
  • 493
  • 1
  • 10
  • `this` is decided at *call time*. In `a.b()`, `this` inside `b` is `a`, that’s basically the rule. – deceze Mar 03 '21 at 06:28

0 Answers0