liveBook · Manning

Delegation

As explained in The Joy of JavaScript

A composition style in which an object forwards behavior or property lookup to another object through delegation links rather than copying or class inheritance.

Delegation lets an object use behavior held by another object. In JavaScript, a missing property or method can be found through a prototype link, so the objects remain separate rather than copying behavior into one place. This supports reuse without making classes the default design model. section 3.0 section 3.1.1

Objects Linked to Other Objects (OLOO) makes those links the design model: objects are peers that delegate work to one another, not children described as deriving from a base class. `Object.create(otherObject)` establishes a link while leaving prototype-chain setup to the language. The link is still a `[[Prototype]]` link; OLOO changes how the relationship is expressed and understood, not the underlying lookup mechanism. section 3.2

Linking a store and a chain

`const Blockchain = Object.create(MyStore)` links one object to shared storage behavior. `const chain = Object.create(Blockchain)` adds another link; `chain.init(createGenesisBlock)` initializes the working object, and `chain.push(new Block(...))` uses behavior available along the links. `init` writes to `this`, so the initialized state belongs to `chain`. section 3.2

const MyStore = {
  init(element) { 
     this.length = 0;
     this.push(element);
  },
  push(b) {
     this[this.length] = b;
     return ++this.length;
  }
}

const Blockchain = Object.create(MyStore);
 
const chain = Object.create(Blockchain);
chain.init(createGenesisBlock);
chain.push(new Block(...));
chain.length; // 2

The listing shows the two `Object.create` links and the separate initialization of the working object. section 3.2

The form of the link changes how delegation reads in code:

  • With an implicit prototype link, a method can call `this.format(msg)`; property lookup finds `format` along the chain while retaining the caller's `this` context. Classes and constructor functions also use this mechanism. section 3.1.1
  • With an explicit reference, a method calls `this.formatter.format(msg)`. This is a visible “uses” relationship between objects with separate life cycles; the reference may need checking before use. section 3.1.2
  • Mixins offer a different reuse choice: they combine independent behavior into an object instead of relying on a longer lookup chain. `Object.assign` and object spread support this compositional approach. section 3.0 section 3.6

For domain objects, choose links when shared behavior should remain on related objects and be reached through lookup. A transaction can link to general transaction behavior while adding its own hashing method. Choose explicit references when the dependency should be visible as a separate collaborator, or mixins when combining independent capabilities into an object better fits the design. section 3.2 section 3.1.2

How The Joy of JavaScript covers it

How deep it goes
Works through Object.create links, prototype lookup, and initialization; contrasts explicit references without developing that technique further.
Examples
Shows a linked blockchain store and transaction objects, plus implicit and explicit formatter calls.
What it assumes
Assumes familiarity with JavaScript objects and methods; builds on prototype-chain scaffolding before applying Object.create to linked objects.
What you can do after
Build it from scratch with Object.create links and initialization methods, then distinguish prototype delegation from explicit references and mixin composition.
How it connects
Connects delegation to prototype chains, property lookup, classes, object composition, and mixins.

Also explained in

Compare how 2 books explain delegation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.