Published on
View count
4 views

Prefer ES6 classes over ES5 functions

Authors

☑️ Topic: Classes

☑️ Idea: ES6 is better than ES5 for readable class inheritance, construction and methods.

☑️ Benefits: Readability, Maintainability.

☑️ Guideline: Use small functions when you need something simple and use ES6 classes when you need larger and more complex objects.

// BAD
const Animal = function (age) {
  if (!(this instanceof Animal)) {
    throw new Error('Instantiate Animal with `new`')
  }
  this.age = age
}
Animal.prototype.move = function move() {}
 
// GOOD
class Animal {
  constructor(age) {
    this.age = age
  }
  move() {
    /* ... */
  }
}