Knowee
Questions
Features
Study Tools

Inheritance and composition difference JavaScript

Question

Inheritance and composition difference JavaScript

🧐 Not the exact question you are looking for?Go ask a question

Solution

Inheritance and composition are two programming concepts that JavaScript supports. They are used to establish relationships between objects and classes.

  1. Inheritance: In JavaScript, inheritance is a concept in which an object is based on another object. It is a way to create a new class (child) from an existing class (parent). The child class inherits all the properties and methods of the parent class. This is achieved through the prototype chain.

For example:

function Parent() {
    this.name = 'parent';
}

Parent.prototype.getName = function() {
    return this.name;
}

function Child() {
    Parent.call(this);
    this.type = 'child';
}

Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

var child = new Child();
console.log(child.getName());  // Outputs: parent

In this example, Child class inherits from Parent class using JavaScript's prototypal inheritance.

  1. Composition: Composition, on the other hand, is a way to combine simple objects or data types into more complex ones. In JavaScript, composition is achieved by giving an object a reference to other objects. Each object takes care of a certain task.

For example:

function eat(food) {
    return {
        eat: function() {
            console.log('Eating ' + food);
        }
    }
}

function sleep(hours) {
    return {
        sleep: function() {
            console.log('Sleeping for ' + hours + ' hours');
        }
    }
}

function person(name, food, hours) {
    var obj = {
        name: name
    }
    return Object.assign(obj, eat(food), sleep(hours));
}

var bob = person('Bob', 'Pizza', 8);
bob.eat();  // Outputs: Eating Pizza
bob.sleep();  // Outputs: Sleeping for 8 hours

In this example, person is composed of eat and sleep behaviors.

In conclusion, the main difference between inheritance and composition in JavaScript is that inheritance is used to create a new class from an existing class, while composition is used to combine simple objects into more complex ones.

This problem has been solved

Similar Questions

What is the primary mechanism for inheritance in JavaScript?Explanation:A) Class-based inheritanceB) Prototypical inheritanceC) Module-based inheritanceD) File-based inheritance

How does prototypal inheritance work in JavaScript?(2 Points)It allows objects to inherit properties from their prototypesIt creates new prototypes for each objectIt is not supported in JavaScriptIt copies properties from one object to another

How do Javascript primitive/object types passed in functions? By Value By Reference None of the above Both A and B

7What are first-class objects in JavaScript?Review LaterFunctions that can be passed as arguments to other functionsObjects that can be stored in a variableBoth A and BNeither A nor B

What is a prototype in JavaScript?

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.