liveBook · Manning

Class

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A language construct that defines a blueprint for creating objects with shared behavior and state.

Suppose an application needs separate stores for users, teams, and products. A class can define the setup and methods those stores share, while each instance holds its own resource. In JavaScript, a class is a compact syntax for a constructor function and its prototype; it does not make JavaScript statically typed. section 27.1 section 27.0

Define and create a store

class DataStore { constructor(resource) { this.resource = resource; } fetch() { // fetch records for this.resource } } const userStore = new DataStore('users'); The class body contains methods. `new` creates the instance and automatically calls `constructor('users')`; there, `this` refers to the new instance. Calling `DataStore('users')` without `new` throws a TypeError. section 27.1 section 27.2

Class methods resemble shorthand methods in object literals, but a class body is not an object literal: write `fetch() { ... }`, not `fetch: function() { ... }`, and do not put commas between methods. Methods are not automatically bound to an instance. section 27.1 section 27.7

For values shared across instances, a static property or method belongs to the class object rather than an instance or its prototype. For example, `DataStore.domain` can hold an API domain while `DataStore.url(path)` builds a URL used during construction. A class can also be exported from a module as either a default or a named export. section 27.6 section 27.3

To customize an existing class, use `extends`. JavaScript classes still use prototypal inheritance; `super` can call superclass methods or its constructor. In a subclass constructor, call `super` before referring to `this`. Instance properties cannot be accessed with `super`. section 28.0 section 28.4

class Car {
  constructor() {
    this.gas = 50;
    this.milage = 0;
  }

  hasGas() {
    return this.gas > 0;
  }

  drive() {
    if (this.hasGas()) {
      this.milage++;
      this.gas--;
    }
  }
}

Practice: extend this `Car` class with a `fuel()` method that resets gas to 50. Override `drive(miles)` and call the superclass method enough times to drive that many miles. section 28.4

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Works through declaration, instantiation, exports, static members, and extension; notes method binding and constructor rules.
Examples
Uses API resource stores, a pet constructor check, and a car-extension exercise.
What it assumes
Assumes familiarity with object methods and constructor functions while teaching class declaration syntax directly.
What you can do after
Build it from scratch: define, instantiate, export, and extend a JavaScript class.
How it connects
Connects classes to constructor functions, prototypes, modules, static properties, and prototypal inheritance.

Also explained in

Compare how 21 books explain class →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.