We Need to Talk About Composition Patterns in Object-Oriented JavaScript

Because patterns matter

Aphinya Dechalert
Modules&Methods

--

Image composed by Aphinya Dechalert. Titled: How outdated is inheritance?

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…

  1. Everything is an object.
  2. Objects are written as classes.
  3. 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:

--

--

Aphinya Dechalert
Modules&Methods

Where Development Meets Storytelling: Tech Writer, Editor & Dev Advocate. Translating Complexity into Clarity. DM me. linkedin.com/in/dechalert