DEV Community

Sosanya Esther
Sosanya Esther

Posted on • Edited on

Inheritance in JavaScript

In programming, inheritance refers to passing down characteristics from a parent to a child so that a new piece of code can reuse and build upon the features of an existing one. JavaScript implements inheritance by using objects.
Inheritance allows you to define a class and then take all the functionality and features of the class and give them to another. The one who gives the features is called the parent class or superclass and the one who inherits the feature is called a child class or subclass. By using inheritance, a class can inherit all the required methods and can also have its unique features.Each object has an internal link to another object called its prototype.
Types of Inheritance in JavaScript
1.Prototypal Inheritance: Prototypal inheritance is a type of inheritance that adds new properties and methods to an existing object.
function Animal(name) {
this.name = name;
}

Animal.prototype.sound = function() {
console.log("Some generic sound");
};

function Dog(name, breed) {
Animal.call(this, name);
this.breed = breed;
}

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

Dog.prototype.sound = function() {
console.log("Woof! Woof!");
};

const myDog = new Dog("Buddy", "Labrador");
myDog.sound(); // Outputs: Woof! Woof!
2.Pseudoclassical Inheritance: Psuedoclassical inheritance is similar to prototypal inheritance.In Pseudo classical inheritance, it emulates the classical inheritance by using prototypal inheritance.
class automobile {
constructor(name, cc) {
this.name = name;
this.cc = cc;
}
engine() {
console.log(${this.name}
has ${this.cc} engine
);
}
}

class car extends automobile {
engine() {
console.log(this.name,
"has ", this.cc, "cc engine");
}
}

let carz = new car('Rex', "1149");
carz.engine();
3.Functional Inheritance: From the name of the inheritance, you understand that this type of inheritance in JavaScript makes use of functions. Functional Inheritance uses an augmenting function to the instance of the object in order to inherit the features of that object.
function Animal(name) {
const obj = {};
obj.name = name;

obj.sound = function() {
    console.log("Some generic sound");
};

return obj;
Enter fullscreen mode Exit fullscreen mode

}

function Dog(name, breed) {
const obj = Animal(name);
obj.breed = breed;

obj.sound = function() {
    console.log("Woof! Woof!");
};

return obj;
Enter fullscreen mode Exit fullscreen mode

}

const myDog = Dog("Buddy", "Labrador");
myDog.sound(); // Outputs: Woof! Woof!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →