Avoid conditionals: Favor Composition over Inheritance (Classes)
Avoid conditionals: Favor Composition instead of Classes
It's preferred for newcomers to learn about composition first, and Classes as a syntax sugar on top of Prototypal inheritance later.
As explained by @ericelliott:
Inheritance is fundamentally a code reuse mechanism: A way for different kinds of objects to share code. The way that you share code matters because if you get it wrong, it can create a lot of problems, specifically:
Class inheritance creates parent/child object taxonomies as a side-effect.
Those taxonomies are virtually impossible to get right for all new use cases, and widespread use of a base class leads to the fragile base class problem, which makes them difficult to fix when you get them wrong. In fact, class inheritance causes many well known problems in OO design:
- The tight coupling problem (class inheritance is the tightest coupling available in oo design), which leads to the next one…
- The fragile base class problem
- Inflexible hierarchy problem (eventually, all evolving hierarchies are wrong for new uses)
- The duplication by necessity problem (due to inflexible hierarchies, new use cases are often shoe-horned in by duplicating, rather than adapting existing code)
- The Gorilla/banana problem (What you wanted was a banana, but what you got was a gorilla holding the banana, and the entire jungle)
https://medium.com/javascript-scene/master-the-javascript-interview-what-s-the-difference-between-class-prototypal-inheritance-e4cd0a7562e9
The best alternative to class inheritance is to use modules and functions as a code reuse mechanism.