liveBook · Manning

Concatenative object extension

As explained in The Joy of JavaScript

An object-composition pattern that extends an object by copying additional properties into it after its initial creation.

Concatenative object extension builds an object by copying properties from independent mixins into a target after its initial creation. Unlike prototype delegation, it composes several fine-grained behavior and data slices directly into the object’s API. section 3.4 section 3.6

Use the pattern when:

  • A target needs several independent capabilities, such as bread, toppings, meat, hashing, or validation. section 3.4 section 3.5
  • Each mixin has a narrow focus and remains self-sustaining, even if it looks incomplete by itself. section 3.4
  • You want structural composition across object, OLOO, class, prototype, or individual-instance styles rather than inheritance. section 3.5 section 3.6

Composing a sandwich

const Sandwich = (size = '6', unit = 'in') => Object.assign({ size, unit }, HasBread, HasToppings, HasTomatoes, HasMeat); A call such as Sandwich(1, 'ft') produces an object whose copied properties include tomatoe: 'Cherry'. The same composition can use object spread instead.

How The Joy of JavaScript covers it

How deep it goes
Explains Object.assign, spread syntax, mixin focus, and object-style applications, but treats mixins only superficially.
Examples
Uses Sandwich composition, HashTransaction, Transaction, Blockchain, Block, and single-instance mixin assignment.
What it assumes
Assumes familiarity with JavaScript objects and prototypes while demonstrating composition through concrete object, OLOO, and class examples.
What you can do after
Build it from scratch by composing objects, prototypes, classes, or instances with Object.assign and focused mixins.
How it connects
Connects mixins with prototypes, behavior delegation, OLOO, object composition, inheritance, and multiple inheritance.

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.