← All tutorials

GoCareerGo Tutorials

JavaScript Tutorial

Variables to closures, promises, the DOM and OOP — a complete JS reference.

ES6 Classes

Syntactic sugar over JavaScript's existing prototype-based inheritance, giving OOP code a familiar class syntax.

class Animal {
  constructor(name) {
    this.name = name;
  }
  speak() {
    return `${this.name} makes a sound.`;
  }
}

const dog = new Animal('Rex');
dog.speak(); // 'Rex makes a sound.'
  • Classes are NOT hoisted the same way functions are (they stay in the TDZ)
  • Class bodies always run in strict mode
  • Methods defined on the class live on the prototype, not each instance