Should you prefer composition or inheritance in JavaScript?
Composition is often preferred. Instead of deep inheritance chains, mix objects: Object.assign(bird, canFly, canSwim). This is more flexible and avoids the problems of deep inheritance (fragile base class, tight coupling).
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Prototypal vs Classical Inheritance
Classical: classes inherit from classes (static, strict, compile-time). Prototypal: objects inherit from objects (dynamic, flexible, runtime). JS uses prototypal. ES6 classes are syntactic sugar over prototypes.
Yes. Use Object.setPrototypeOf(obj, newProto). This is possible because JS uses prototypal inheritance (dynamic). In classical inheritance, the hierarchy is fixed at compile time.
Prototypal. ES6 classes are syntactic sugar over the prototype system. Under the hood, it is the same: methods go on the prototype, extends sets up the prototype chain. The class syntax looks classical but the mechanism is prototypal.
Still have questions?
Browse all our FAQs or reach out to our support team
