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.
