π Prototypes & Prototypal Inheritance in JS: Let's Learn Together (and Nail That Interview π―)

Table of contents
- π οΈ What are Prototypes?
- π Key Points:
- π Understanding Prototype with an Example π€
- π Key Points about Prototype in JavaScript π
- πΎ Understanding Prototypes with Dog and Animal Example πΆ
- π Key Concepts:
- π¬ How It Works:
- π Summary:
- π Real-World Analogy: Car and ElectricCar πβ‘
- π Parent (Prototype) - Car
- β‘ Child (Object inheriting from Car) - ElectricCar
- π¬ How It Works:
- π Summary:
- π Conclusion:
- π‘ Key Takeaways:

π οΈ What are Prototypes?
In JavaScript, objects can inherit ποΈ properties and methods from other objects using prototypes π.
π Key Points:
βοΈ Every object in JavaScript has a special property called a prototype π·οΈ.
βοΈ This prototype is itself another object 𧩠and can have its own prototype.
βοΈ This forms a prototype chain π, where objects inherit features from one another.
βοΈ The prototype chain ends π« when an objectβs prototype is null
(meaning no further inheritance β).
π Understanding Prototype with an Example π€
π Example 1: Using Prototype to Add Methods π οΈ
// Constructor function
function Person(name, age) {
this.name = name;
this.age = age;
}
// Adding a method to Person's prototype
Person.prototype.greet = function() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old. πββοΈ`);
};
// Creating instances of Person
const person1 = new Person("Saurabh", 28);
const person2 = new Person("John", 30);
// Calling the method from prototype
person1.greet(); // Output: Hello, my name is Saurabh and I am 28 years old. πββοΈ
person2.greet(); // Output: Hello, my name is John and I am 30 years old. πββοΈ
Here, the greet
method is added to Person.prototype
, meaning all instances of Person
can access it without duplicating the method in each instance. π
π Example 2: Checking the Prototype Chain π
console.log(person1.__proto__ === Person.prototype); // true β
console.log(Person.prototype.__proto__ === Object.prototype); // true β
console.log(Object.prototype.__proto__); // null β
This shows:
person1
's prototype isPerson.prototype
. πΌPerson.prototype
inherits fromObject.prototype
. πObject.prototype
has no further prototype (its__proto__
isnull
). π«
π Key Points about Prototype in JavaScript π
Every function in JavaScript has a
prototype
property, which is an object. π§βπ»Objects created from a constructor function inherit properties and methods from that constructor's prototype. π
Prototype-based inheritance allows memory-efficient sharing of methods. π§ πΎ
The prototype chain helps us look up properties/methods in parent objects. π
πΎ Understanding Prototypes with Dog and Animal Example πΆ
Letβs break it down step-by-step using the Animal (Parent) and Dog (Child) objects. πΎ
π Key Concepts:
Parent (Prototype) - Animal π¦
The Animal object has some properties and methods that all animals (including dogs) will have. This acts like a template for all animals.const Animal = { type: "Animal", // Type of animal eats: true, // All animals eat makeSound: function() { // Generic sound console.log("Generic animal sound"); } };
Child (Object inheriting from Animal) - Dog π
The Dog object inherits all properties and methods from Animal usingObject.create()
. Dogs have their own properties and can override the inherited methods.const Dog = Object.create(Animal); // Dog inherits from Animal Dog.breed = "Golden Retriever"; // Dog's own property Dog.makeSound = function() { // Dog's own sound console.log("Woof!"); // Overrides the inherited sound };
π¬ How It Works:
Inheriting Properties from Animal π¦:
- Dog inherits the
type
andeats
properties from Animal.
- Dog inherits the
console.log(Dog.type); // Output: "Animal" (inherited)
console.log(Dog.eats); // Output: true (inherited)
Dog's Own Property πΎ:
- The Dog has its own property
breed
.
- The Dog has its own property
console.log(Dog.breed); // Output: "Golden Retriever" (Dog's own property)
Overriding a Method π€:
- The Dog overrides the inherited
makeSound
method to make its own unique sound, "Woof!" πΆ.
- The Dog overrides the inherited
Dog.makeSound(); // Output: "Woof!" (Dog's own method overrides Animal's)
Prototype Chain π:
Animal.isPrototypeOf(Dog)
checks if Animal is in the prototype chain of Dog. Since Dog inherits from Animal, it returnstrue
.
console.log(Animal.isPrototypeOf(Dog)); // Output: true (Animal is prototype of Dog)
π Summary:
Dog π inherits type and eats from Animal π¦, but can also have its own properties like
breed
.Dog π can override the inherited
makeSound
method to make its unique sound, "Woof!" πΎ.Prototype chain π allows objects to inherit properties and methods, making code more reusable and efficient.
π Real-World Analogy: Car and ElectricCar πβ‘
Imagine we have a Car (parent) object that has properties and methods for any car. Then, we have an ElectricCar (child) object that inherits from the Car object but also has its own properties, like being electric.
π Parent (Prototype) - Car
The Car object is the base prototype with general properties and methods that all cars will have.
// Parent (Prototype)
const Car = {
type: "Car", // Type of vehicle
wheels: 4, // All cars have 4 wheels
fuel: "Gasoline", // Cars use gasoline (default)
startEngine: function() { // All cars can start their engine
console.log("Engine started π");
}
};
β‘ Child (Object inheriting from Car) - ElectricCar
The ElectricCar object inherits the properties and methods from Car but can also add its own features, like using electricity instead of gasoline πβ‘.
// Child (Object inheriting from Car)
const ElectricCar = Object.create(Car);
// Adding ElectricCar's own property
ElectricCar.battery = "Lithium-ion battery π";
// Overriding the fuel property and method
ElectricCar.fuel = "Electricity β‘";
ElectricCar.startEngine = function() {
console.log("Electric engine started πβ‘");
};
π¬ How It Works:
Inheriting Properties from Car π:
- The ElectricCar inherits the
type
,wheels
, andfuel
properties from Car.
- The ElectricCar inherits the
console.log(ElectricCar.type); // Output: "Car" (inherited from Car)
console.log(ElectricCar.wheels); // Output: 4 (inherited from Car)
console.log(ElectricCar.fuel); // Output: "Electricity β‘" (overridden by ElectricCar)
ElectricCar's Own Property π:
- The ElectricCar has its own property
battery
.
- The ElectricCar has its own property
console.log(ElectricCar.battery); // Output: "Lithium-ion battery π" (ElectricCar's own property)
Overriding a Method β‘:
- The ElectricCar overrides the
startEngine
method to reflect the electric engine start.
- The ElectricCar overrides the
ElectricCar.startEngine(); // Output: "Electric engine started πβ‘" (ElectricCar's custom method)
Prototype Chain π:
- The Car object is still in the prototype chain of ElectricCar. So, ElectricCar can access all the inherited properties and methods from Car.
console.log(Car.isPrototypeOf(ElectricCar)); // Output: true (Car is prototype of ElectricCar)
π Summary:
The ElectricCar ποΈ inherits properties like
type
,wheels
, andfuel
from Car π, but it can override thefuel
property and thestartEngine
method to fit its electric nature β‘.ElectricCar also adds its own property,
battery
, to reflect its specific feature π.
This is how prototype inheritance allows objects to share properties and methods, while also enabling customization and extensions! π
π Conclusion:
Inheritance and Prototypes in JavaScript:
- Prototypes are like a family tree π³ in JavaScript. They allow objects to inherit properties and methods from other objects, saving you from repeating code and making it more reusable and efficient. β‘
Example 1: Animal and Dog π¦π:
The Dog inherits general properties like
type
andeats
from its parent, Animal. But it can also add its own properties likebreed
and override methods (e.g.,makeSound
) to be more specific to Dog. This shows how an object can extend and customize the features it inherits.This is like a child inheriting basic traits (e.g., eating) from their parents but also gaining their own unique skills (like dancing or playing the guitar) πΈ.
Example 2: Car and ElectricCar πβ‘:
The ElectricCar inherits general properties like
wheels
andfuel
from the Car prototype. It then overrides thefuel
to be Electricity and changes thestartEngine
method to reflect its electric engine. The ElectricCar also adds its own property (battery
) to reflect its unique feature.This is similar to a child inheriting characteristics from a parent (like the number of wheels) but adapting and adding new features (like an electric engine) to make it their own.
π‘ Key Takeaways:
Prototypes allow objects to inherit properties and methods, avoid duplication, and enable code reuse.
Objects can override inherited methods and add their own properties to extend or customize their behavior.
The prototype chain links objects together, forming a hierarchical structure where each object can inherit from its parent and its parentβs parent, up to the top (usually
Object.prototype
).
This is the magic of inheritance in JavaScript! It helps you write cleaner, more organized code while keeping things modular and flexible. π
Subscribe to my newsletter
Read articles from Saurabh Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
