JavaScript theorytheory 0/50 · 0%
Objects · medium

26. Classes and prototypes

Syntax sugar over the prototype chain.

A class defines a constructor plus methods stored on the prototype, shared by every instance. Private fields start with `#` and are inaccessible outside the class.

class Wallet {
  #key;
  constructor(key) { this.#key = key; }
  get address() { return derive(this.#key); }
}

`this` depends on the call site; bind methods or use arrow properties when passing them as callbacks.

Check your understanding

  1. 1. Where do class methods live?

  2. 2. What does a `#` field give you?

  3. 3. Why can `this` be undefined in a callback?