0

I`m learning function constructor at the moment. This seems to work fine.

Person.prototype.calculateAge = function () {
  console.log(2020 - this.yearOfBirth);
};

Linter doesn`t like when I use anonymous functions and offers arrow functions instead. When I modify it:

Person.prototype.calculateAge = () => {
      console.log(2020 - this.yearOfBirth);
    };

I get NaN. Thank you for your help!

  • Arrow functions make poor class methods because they inherit the scope `this`, instead of pointing to the instance. – Mitya Sep 17 '20 at 14:31
  • Is theres a good way to avoid anonymous functions in function prototypes?? – Valery Dauzhuk Sep 17 '20 at 14:46
  • No - and there's no real reason why you'd need them to be. They'll always be methods of the object's `prototype`. – Mitya Sep 18 '20 at 09:37

0 Answers0