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




















