3 Linked, compositional object models

 

This chapter covers

  • Understanding the Objects Linked to Other Objects (OLOO) pattern of behavior delegation with linked objects
  • Combining classes with mixins for concatenative dynamic extension
  • Using Object.assign and the spread operator to build new objects

Class inheritance is very rarely (perhaps never) the best approach in JavaScript.

—Eric Elliot

In chapter 2, we looked at some of the scaffolding needed to create prototype chains to model inheritance and how classes streamline this process. Remember that the goal of using inheritance is to improve reusability. Now we’ll continue the topic of assembling your objects to achieve the same level of code reuse, but in a way that doesn’t require you to think in terms of inheritance.

The first technique, discovered by Kyle Simpson, is called Objects Linked to Other Objects (OLOO) and relies on Object.create to create associations among the objects that constitute your domain model. This technique has the simplicity of classes of stripping away the complicated prototype jargon while setting up the prototype chain properly. This pattern is interesting because it allows you to look at your domain model as a collection of peer objects that delegate to one another to carry out their work.

3.1 Types of object links

3.1.1 Implicit

3.1.2 Explicit

3.2 OLOO

3.3 Understanding Object.assign

3.3.1 Object.assign uncovered

3.3.2 Assignment vs definition

3.4 Assembling objects using mixin composition

3.4.1 Anatomy of a mixin

3.4.2 Multiple inheritance and linearization

3.4.3 Composing objects using Object.assign and the spread operator

3.5 Applying shared mixins to multiple objects

Summary