Object composition is the process of creating a composite object.
An object formed from primitive and/or other composite objects.
These forms of composition are not mutually exclusive.
When you form an object that is an enumerable collection of sub objects. Each subobject must retain its own identity so that it can be destructured from the aggregate without loosing any of the sub objects information.
Arrays, Maps, Sets, Graphs, Trees, DOM nodes
When you want to apply opertions to a collection of objects.
If memory is an issue because of thousands or millions of sub objects, consider using streams as they will be more memory effecient.
Forming an object by adding new properties to an existing object.
- jQuery.fn allows you to extend jQuery
- adding properties via object.assign or the spread operator.
When you want to progressively build an object at runtime. Merging objects, creating update to immutable state.
- Becareful mutating existing objects.
- Avoid is-a relationships.
- Avoid implicit dependencies
- Key collisions, last in wins.
When an object forwards or delegates to another object.
JavaScript Array and Object instance delegate to Array.prototype and Object.prototype
Creating instances from a 'master' object. If the master is updated all instances update at runtime.
- Ivan Sutherlands Sketchpad ( 1962 )
- Photoshop smart objects
- To conserve memory.
- Dynamically update many instances. Dynamic Delegation
- Trading memory for computation to lookup delegates.
- Need to define instance vs delegate state.
- Shared state is not instance safe. Share state on dynamic delegates is commonly but not always a bug.
- Avoid is-a relationships.
- Key collisions, last in wins.
- ES6 classes do no support dynamic delegates.
- keys will not be enumerable by Object.keys