2

I've added a method calcAge to jonas, but why is it working for matilda?

const Person = function (firstName, birthYear) {
  this.firstName = firstName;
  this.birthYear = birthYear;
};

const jonas = new Person('jonas', 1991);
const matilda = new Person('Matilda', 2017);

jonas.__proto__.calcAge = function () {
  console.log(2037 - this.birthYear);
};
jonas.calcAge(); // 46
matilda.calcAge(); // 20
ndc85430
  • 1,395
  • 3
  • 11
  • 17

1 Answers1

0

You added the method to proto which all objects inherit from. so jonas.proto and matilda.proto reference the same thing. You can think of it like a family tree.

example: carl.mom and jane.mom if carl and jane are brother and sister. mom is the same person.

hope this makes sense.

Bruck
  • 33
  • 5