What is the difference between inheritance patterns in JavaScript?
Answer
JavaScript supports multiple inheritance patterns, each with trade-offs. Prototype chain (prototypal inheritance): Dog.prototype = Object.create(Animal.prototype) — classic, efficient memory (methods shared), but verbose. ES6 Classes: class Dog extends Animal — cleaner syntax, same prototypal mechanism underneath. Mixin pattern: copy methods from multiple sources onto a prototype — multiple "inheritance": Object.assign(Dog.prototype, CanMixin, SwimMixin). No deep hierarchy, compose capabilities. Factory functions with composition: create objects via functions returning plain objects with shared methods via closure or shared prototype — no new, no this confusion: const createDog = (name) => ({ name, bark: () => "Woof", ...animalMethods }). Delegation: objects delegate behavior to other objects via composition rather than inheritance: const dog = Object.create(animal); dog.bark = () => "Woof". The JavaScript community increasingly favors composition over inheritance — mixins and factory functions over deep class hierarchies. The "diamond problem" of multiple inheritance is avoided by explicit composition.
Previous
What are JavaScript Symbols advanced usage?
Next
What is the module pattern and its evolution?