Doubt in prototypes

const personPrototype = {
  greet() {
    console.log(`hello, my name is ${this.name}!`);
  }
}

function Person(name) {
  this.name = name;
}

Person.prototype = personPrototype;
Person.prototype.constructor = Person;

In the above code, PersonPrototype is set to Person.prototype, Here what is personPrototype? If its a variable, it should be defined using let right?
How is the code accepting this? plz help me in detailed understanding.

Hi @vaishnavi_A.N

personPrototype is defined on the first line with const. It’s const not let since it won’t change. It’s an object with a function that gets assigned to Person.prototype.

Did I misunderstand anything?

2 Likes

oh sorry for that doubt @mikoMK, I really was so focussed in understanding that I did not see the declaration above.

Nothing, its much clearer, Thank u very much @mikoMK.

1 Like