Prototype/class based object construction

I skipped ahead of the ES6 module and learned how to create objects using prototypes in the object-orientated module. I have now gone back to ES6 and it is demonstrating an updated method to constructing objects using classes. Learning both of these at once is confusing, is there one which I should focus upon, or do they both have their place? Would a specific approach have an advantage? there are mixed articles online.

The class syntax is syntactic sugar for the other, they’re literally the same, the class syntax is just (IME, and I think most would agree) more consistent and convenient.

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

Example.prototype.sayName = function () {
  console.log(this name)
}

Same as

class Example {
  constructor (name) {
    this.name = name;
  }

  sayName() {
    console.log(this.name);
  }
}
2 Likes

This is a really nice summary, thank you

1 Like