Using composition rather than inheritance to add behavior

So far, we've seen how to use inheritance to add behavior and compose larger structures. This is not always the ideal approach. In many situations, it's better to use a method known as composition instead. This involves using distinct classes in connection without establishing a hierarchical relationship. The chief advantage here is code clarity and flexibility.

In this recipe, we'll see how to use composition.

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.