Sobes.tech
Middle+

What type of inheritance is used in JavaScript? Is there multiple inheritance in classes?

sobes.tech AI

Answer from AI

JavaScript uses prototypal inheritance, and with the introduction of classes (ES6), it provides syntactic sugar over prototypes. Classes in JS support single inheritance, meaning a class can only inherit from one parent.

JavaScript does not support multiple inheritance for classes, but mixins can be used — functions that add methods to a class to imitate multiple inheritance.

Example of single inheritance:

class Animal {
  speak() {
    console.log('Animal sound');
  }
}

class Dog extends Animal {
  speak() {
    console.log('Woof!');
  }
}

const dog = new Dog();
dog.speak(); // Woof!

Example of a mixin:

const CanFly = Base => class extends Base {
  fly() {
    console.log('Flying');
  }
};

class Bird {}
class Eagle extends CanFly(Bird) {}

const eagle = new Eagle();
eagle.fly(); // Flying
What type of inheritance is used in JavaScript? Is… - sobes.tech