Inheritance
Inheritance lets one class reuse another class's methods and fields. In JavaScript, extends wires the prototype chain of both the instances and the constructors.
Detailed Explanation
class Admin extends User does two prototype links. Admin.prototype delegates to User.prototype, so an admin instance finds describe on User if Admin did not override it. Admin itself delegates to User, so static methods are also inherited. new Admin() creates an object whose [[Prototype]] is Admin.prototype.
The subclass constructor must call super(...) before it touches this. Until super returns, the instance does not exist and this is in a temporal dead zone. Forgetting super is a ReferenceError. If you omit the constructor entirely, the default is constructor(...args) { super(...args); }.
Overriding a method is writing the same name on the subclass. The subclass version hides the parent version on that instance's lookup. Call the parent version with super.method(...) inside a class method. super is not an object you can pass around; it is syntax bound to the method's home class.
instanceof follows the instance prototype chain. admin instanceof Admin and admin instanceof User are both true. Array.isArray is the safer test for arrays because inheritance across realms breaks instanceof.
JavaScript has single inheritance of classes. You cannot extends two classes. Mixins, delegation, and composition are the alternatives when you want shared behaviour from several places. extends can target any constructor with a .prototype, including Array, Error, and a function constructor. Extending null (class Dict extends null) is a special case that produces an object with no Object.prototype, and it is awkward enough that Object.create(null) is usually clearer.
What it costs you: coupling. A subclass depends on the parent's constructor arguments, field names, and the order of super. Deep hierarchies are hard to refactor. A change to User can break Admin without a compiler to warn you. Prefer a short chain: one base, a few leaves. Prefer composition (user.permissions = new Permissions()) when the relationship is "has a", not "is a".
Reach for extends when the subtype is the parent type and must pass instanceof and shared methods. Reach for a wrapper object when you only need some of the behaviour. Reach for a mixin when several unrelated classes need the same small protocol.
Code Examples
class User {
constructor(email) {
this.email = email;
}
describe() {
return this.email;
}
}
class Admin extends User {
constructor(email, level) {
super(email); // must run before `this`
this.level = level;
}
describe() {
return `${super.describe()} (admin ${this.level})`;
}
}
const ada = new Admin("ada@example.com", 2);
console.log(ada.describe()); // => ada@example.com (admin 2)
console.log(ada instanceof Admin); // => true
console.log(ada instanceof User); // => trueclass TokenError extends Error {
constructor(code, message) {
super(message);
this.name = "TokenError";
this.code = code;
}
}
const err = new TokenError("expired", "sign in again");
console.log(err instanceof TokenError); // => true
console.log(err instanceof Error); // => true
console.log(err.message); // => sign in againCommon Gotchas
Use `this` before `super()` in a subclass and it throws
the instance is not allocated yet. Call `super` first, then assign fields.
Static members inherit too
`class Admin extends User` means `Admin.create` falls through to `User.create` unless you override it.
Extending built-ins has engine-specific corners
`class MyArray extends Array` usually works; exotic objects such as `Promise` need care with species and `then`.
There is no multiple class inheritance
compose objects or apply a mixin helper instead of stacking `extends`.