Let’s begin with Mixins
One of the ways is mixins. This is essentially coping properties and methods from one object to another. It can be achieved by using ES6
Object.assign() which copies one or more objects and returns a new object. Lodash
_.extend() achieves the same result if you need older browser support.
Below is an example of creating a mixin:
The next tactic is to use functional inheritance similar to mixins but you can use closures to enforce private data. We can then utilise privilege methods to manipulate the private data fields. Refactoring the example above to use functional inheritance:
Composition creates a different relationship
Classical inheritance typically creates an is-a relationship but in a way the mental model of has-a or uses-a is easier to grasp, which is achieved using composition.
const lambo = hasA(accelerator, brake, steeringWheel);
Constructing code to be composable I believe makes it easier to reason about which should improve its readability. There is a little overhead of creating boiler code for example
lamboShell object is not particularly useful until it is composed. However, this tradeoff I think is worth it to make the code easier to follow, especially when an application becomes complex.