We Need to Talk About Composition Patterns in Object-Oriented JavaScript
Because patterns matter
At the end of the day, code is code. However, the difference between effective code and the kind that sends developers off to consume passive-aggressive memes is how the code itself is written.
What many tend to forget is that code is a language, which means there are patterns and structures required in order to communicate their meaning. Some patterns and structures are simply better than others.
When it comes to flexibility and modularity, composition patterns are more efficient than inheritance patterns in the long run. Many of us default to inheritance patterns because it’s easy, and most of the time, it is also one of the first things we learn when we start hitting object-oriented patterns.
Before we dive any further, let us begin with what exactly object-oriented is.
The Idea of Objects
The concept of object-oriented goes…
- Everything is an object.
- Objects are written as classes.
- Every class of an object has properties and methods.
And that’s where most courses and tutorials stop. In JavaScript, it sometimes can look something like this: