liveBook · Manning

Prototype-based object orientation

As explained in The Joy of JavaScript

An inheritance style in which an object delegates property and method lookup to objects linked in its prototype chain.

Prototype-based object orientation links objects so one can use properties and methods found on another. In JavaScript, a property lookup can follow an object’s internal prototype link up a chain. This is delegation: the linked object supplies behavior without its properties first being copied onto the receiving object. section 2.1 section 3.1.1

Listing 2.2 Transaction objects linked by a basic prototype setup
const transaction = {                                           #1
   sender: 'luis@tjoj.com',
   recipient: 'luke@tjoj.com'
};
 
const moneyTransaction = Object.create(transaction);            #2
moneyTransaction.funds = 0.0;
moneyTransaction.addFunds = function addFunds(funds = 0) {      #3
  this.funds += Number(funds);
}
 
moneyTransaction.addFunds(10.0);
moneyTransaction.funds; // 10.0

The transaction example uses Object.create(transaction) to link a money transaction to a shared object, then adds funds and addFunds directly to the new object. section 2.1

The link is implicit in ordinary property access. A money transaction can read a sender supplied by transaction, while its own addFunds method updates its funds through this. Object.getPrototypeOf(moneyTransaction) can verify the link. Delegated methods use the receiving object’s context, so a method found higher in the chain can work with the object on which it was called. section 2.1 section 3.1.1

Choose an object-building style with its underlying behavior in mind:

  • Object.create establishes a direct link to a chosen prototype. section 2.1
  • Constructor functions and classes also use implicit prototype links; class syntax smooths over their configuration rather than replacing the mechanism. section 2.4 section 3.1.1
  • Object.assign can instead assemble an object from copied properties. Copying avoids depending on a long delegation chain, though it is a different form of reuse. section 3.3

A shared prototype makes behavior reusable, but a change to a base object can affect objects farther down its delegation chain. Keep that coupling in view when modeling a domain: prototypes are useful for more than parent–child relationships, and copying or composing properties may better suit a model that needs to change. section 2.0 section 3.3

How The Joy of JavaScript covers it

How deep it goes
Works through Object.create, prototype inspection, and implicit property lookup, while contrasting linking with copied properties.
Examples
Uses linked transaction and money-transaction objects to show shared sender data and an addFunds method.
What it assumes
Assumes familiarity with JavaScript objects and some exposure to prototypes, then establishes a basic prototype chain.
What you can do after
Build it from scratch with Object.create and evaluate when delegation or property copying better fits an object model.
How it connects
Connects prototype chains to delegation, constructor functions, classes, mixins, and Object.assign.

Also explained in

Compare how 2 books explain prototype-based object orientation →

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.