DEV Community

Discussion on: Understanding Prototypal Inheritance in JavaScript : Part 1

Collapse
 
karataev profile image
Eugene Karataev

I think if you're working with classes then dealing with prototypes directly is not necessary and might be confusing.
Person example can be rewritten without direct access to the prototype chain:

class Person {
  constructor() {
    this.birthYear = 2010;
  }


  calculateAge() {
    return new Date().getFullYear() - this.birthYear;
  }

  incrementBirthYearByOne() {
    this.birthYear += 1;
  }
}

In this case the birthYear property is created in the person instance, not in the prototype while calculateAge and incrementBirthYearByOne are in the prototype.