Eric Elliott
1 min readFeb 4, 2016

--

It’s all semantics…

All forms of inheritance in JavaScript (including class inheritance) are built on top of either delegation or concatenation, or both. However, if you avoid class inheritance (is-a relationships with parent/child hierarchies), your code will be immune to its problems.

In JavaScript, class inheritance is implemented on top of prototypal inheritance, but that does not mean that it does the same thing:

JavaScript’s class inheritance uses the prototype chain to wire the child`[[Prototype]]` to the parent `[[Prototype]]` for delegation. Usually, the`super()` constructor is also called. Those steps form single-ancestor parent/child hierarchies and create the tightest coupling available in OO design.

In the case of the stampit example, the same sort of tight coupling happens because a descendant is given knowledge of a parent.

Class inheritance always creates parent/child hierarchies. The same is not true of composition.

When I say “composition is immune” I do not mean that class inheritance built on top of concatenation is immune. I mean that when you assemble loosely coupled parts to form a whole, you don’t have to worry about class inheritance problems. I’m talking about the semantic meaning of composition, not the mechanical process of concatenation.

Likewise, you can use the `class` keyword and NOT engage in class inheritance (e.g., never `extend` an existing class). But then you’re not using class inheritance.

--

--