Inheritance in JavaScript
Hello! In this guide, we'll explore how inheritance works in JavaScript. Inheritance is a core concept in object-oriented programming (OOP) and allows one class to inherit properties and methods from another. JavaScript, being a prototype-based language, implements inheritance through prototypes. Letβs dive in!
1. Prototypal Inheritanceβ
JavaScript objects have a special hidden property called [[Prototype]]
, which is either null
or references another object. This object is referred to as the prototype. When trying to access a property on an object, JavaScript will first look for it on the object itself. If it's not found, it will look for the property in the prototype chain.
Example:
let animal = {
eats: true,
};
let rabbit = {
jumps: true,
};
rabbit.__proto__ = animal; // Set animal as the prototype of rabbit
console.log(rabbit.eats); // true (inherited from animal)
console.log(rabbit.jumps); // true (own property)
Output:β
true
true
2. Using Object.create()
β
The Object.create()
method creates a new object with the specified prototype object and properties.
Example:β
let animal = {
eats: true,
};
let rabbit = Object.create(animal);
rabbit.jumps = true;
console.log(rabbit.eats); // true (inherited from animal)
console.log(rabbit.jumps); // true (own property)
Output:β
true
true
3. ES6 Classes and Inheritanceβ
In ES6, JavaScript introduced the class
syntax, which is a more convenient way to work with inheritance.
3.1 Defining Classesβ
Classes are defined using the class
keyword, and inheritance is established using the extends
keyword.
Example:β
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}
let dog = new Dog("Rex");
dog.speak(); // Rex barks.
Output:β
Rex barks.
3.2 Super Keywordβ
The super
keyword is used to call the constructor or methods of the parent class.
Example:β
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
class Dog extends Animal {
constructor(name, breed) {
super(name); // Call parent constructor
this.breed = breed;
}
speak() {
super.speak(); // Call parent method
console.log(`${this.name} barks.`);
}
}
let dog = new Dog("Rex", "Labrador");
dog.speak();
Output:β
Rex makes a noise.
Rex barks.
4. Checking Inheritanceβ
You can check if an object inherits from another object using the instanceof
operator or the isPrototypeOf()
method.
4.1 Using instanceof
β
The instanceof
operator checks if an object is an instance of a class.
Example:β
class Animal {}
class Dog extends Animal {}
let dog = new Dog();
console.log(dog instanceof Dog); // true
console.log(dog instanceof Animal); // true
console.log(dog instanceof Object); // true
Output:β
true
true
true
4.2 Using isPrototypeOf()
β
The isPrototypeOf()
method checks if an object exists in another object's prototype chain.
Example:β
let animal = {};
let rabbit = Object.create(animal);
console.log(animal.isPrototypeOf(rabbit)); // true
Output:β
true
5. Overriding Methodsβ
In JavaScript, a subclass can override the methods of its superclass. This is often done to provide specialized behavior.
Example:β
class Animal {
speak() {
console.log("Animal speaks");
}
}
class Dog extends Animal {
speak() {
console.log("Dog barks");
}
}
let dog = new Dog();
dog.speak(); // Dog barks (method overridden)
Output:β
Dog barks
6. Static Methods and Propertiesβ
JavaScript classes can also have static methods and properties, which are called on the class itself, not on instances.
Example:β
class Animal {
static category = "Mammal";
static info() {
console.log("Animals are multicellular organisms.");
}
}
console.log(Animal.category); // Mammal
Animal.info(); // Animals are multicellular organisms.
Output:β
Mammal
Animals are multicellular organisms.
JavaScript inheritance, through prototypes or the class
syntax, allows for powerful code reuse and logical organization. By mastering inheritance, you can create more flexible and efficient code. Happy coding!