Composition over inheritance เป็นหลักการออกแบบที่ถูกอ้างอิงกันอย่างแพร่หลาย โดยสนับสนุนการสร้างออบเจ็กต์ด้วยการ ผสานพฤติกรรมเข้าด้วยกัน (composition) แทนการสืบทอดจาก base classes (inheritance) Inheritance มีข้อเสียที่สำคัญซึ่ง composition หลีกเลี่ยงได้ แม้ทั้งสองจะมีที่ทางของมัน
Inheritance vs composition
INHERITANCE → a class EXTENDS another, inheriting its behavior ("IS-A" relationship):
class Dog extends Animal
COMPOSITION → a class is BUILT FROM other objects/behaviors ("HAS-A" / uses):
class Car { constructor() { this.engine = new Engine(); } } // composes behaviors
→ composition: combine smaller pieces; inheritance: derive from a parent
