What is Prototypes and Inheritance in JavaScript

BetterLife
3 min readFeb 17, 2024

Object-oriented programming (OOP) is a powerful paradigm that enables developers to organize code into reusable and modular components. In JavaScript, prototypes and inheritance play a crucial role in implementing OOP principles. Understanding these concepts is essential for writing efficient, maintainable, and scalable JavaScript code. In this article, we will explore prototypes, inheritance, and their significance in JavaScript development.

Photo by Gabriel Heinzer on Unsplash

The Essence of Prototypes

Prototypes lie at the heart of JavaScript’s object-oriented nature. Every JavaScript object has a prototype, which serves as a blueprint for the object’s properties and methods. When a property or method is accessed on an object, JavaScript first checks if the object itself contains the property or method. If not found, it looks up the prototype chain until it finds the property or method or reaches the end of the chain.

Creating Objects with Prototypes

In JavaScript, prototypes are often created using constructor functions or object literals. Let’s delve into examples to illustrate both approaches:

// Using constructor function
function Person(name, age) {
this.name = name;
this.age = age;
}

Person.prototype.greet = function() {
return `Hello, my name is ${this.name} and I'm ${this.age} years old.`;
};

let john = new Person('John', 30);
console.log(john.greet());
// Output: Hello, my name is John and I'm 30 years old.
// Using object literal

let animal = {
type: 'Unknown',
sound: 'Undefined',
makeSound() {
return `${this.type} makes ${this.sound} sound.`;
}
};

let dog = Object.create(animal);
dog.type = 'Dog';
dog.sound = 'Bark';

console.log(dog.makeSound());
// Output: Dog makes Bark sound.

In the above examples, we create objects (john and dog) that inherit properties and methods from their respective prototypes (Person.prototype and animal).

The Power of Inheritance

Inheritance is a fundamental concept in OOP that allows objects to inherit properties and methods from their parent objects. In JavaScript, inheritance is achieved through prototype chaining, where objects inherit from other objects along a prototype chain.

Prototypal Inheritance in JavaScript

JavaScript employs prototypal inheritance, where objects inherit directly from other objects. This approach contrasts with classical inheritance found in languages like Java or C++, where classes are used as blueprints for creating objects.

Let’s explore inheritance through an example:

// Parent constructor function
function Animal(name) {
this.name = name;
}

// Adding a method to the parent prototype
Animal.prototype.speak = function() {
return `${this.name} makes a sound.`;
};
// Child constructor function
function Dog(name, breed) {
Animal.call(this, name);
this.breed = breed;
}
// Inheriting from the parent prototype
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
// Adding a method to the child prototype
Dog.prototype.bark = function() {
return `${this.name} barks loudly!`;
};
let myDog = new Dog('Buddy', 'Golden Retriever');
console.log(myDog.speak()); // Output: Buddy makes a sound.
console.log(myDog.bark()); // Output: Buddy barks loudly!

In this example, Dog inherits from Animal by setting its prototype to Animal.prototype using Object.create(). This establishes a prototype chain where Dog delegates to Animal for methods like speak.

Conclusion

Prototypes and inheritance are foundational concepts in JavaScript’s object-oriented programming paradigm. By leveraging prototypes, developers can create objects with shared properties and methods, promoting code reuse and modularity. Understanding how inheritance works through prototype chaining enables developers to build complex applications with clear and maintainable code structures.

As you continue your journey in JavaScript development, mastering these concepts will empower you to create robust and scalable applications that adhere to best practices in object-oriented programming. Whether you’re building web applications, server-side applications, or even mobile apps with frameworks like React Native, a solid understanding of prototypes and inheritance will be invaluable in your development endeavors.

Looking for Javascript Opportunities:

Promote Yourself on GetJSJobs:

https://getjsjobs.comReverse Job Board for Javascript Developers

--

--