---
title: "How does prototypal inheritance work in JavaScript?"  
description: "How does prototypal inheritance work in JavaScript?"  
author: "Revati S Misra"  
published: 2023-04-14  
updated: 2023-11-27  
canonical: https://www.mindstick.com/forum/157807/how-does-prototypal-inheritance-work-in-javascript  
category: "javascript"  
tags: ["javascript", "inheritance"]  
reading_time: 2 minutes  

---

# How does prototypal inheritance work in JavaScript?

How does [prototypal inheritance](https://www.mindstick.com/interview/33910/how-does-prototypal-inheritance-work-in-javascript) work in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?

## Replies

### Reply by Aryan Kumar

Prototypal [inheritance](https://www.mindstick.com/articles/13095/inheritance-and-its-types) in JavaScript is a way for objects to inherit properties and methods from other objects through their prototype chain. Unlike classical inheritance in some other programming languages, JavaScript uses a prototype-based model.

Here's a breakdown of how prototypal inheritance works:

## Objects and Prototypes:

- In JavaScript, objects have an internal property called **[[Prototype]]** or **__proto__**, which points to another object.
- This relationship forms a chain, known as the prototype chain.

## Prototype Chain:

- When you access a property or method on an object, JavaScript looks for that property in the object itself.
- If it doesn't find the property, it looks in the object's prototype (referenced by **__proto__**).
- This process continues up the prototype chain until the property is found or until the end of the chain (where the prototype is **null**).

## Constructor Functions and Prototypes:

- Constructor functions are used to create objects with a shared prototype.
- When you create an object using a constructor function with the **new** keyword, the object's **__proto__** is set to the prototype of the constructor function.
- Properties and methods defined on the prototype are shared among all instances created from that constructor function.

Here's a simple example:

```plaintext
// Constructor function
function Animal(name) {
  this.name = name;
}

// Adding a method to the prototype
Animal.prototype.sayHello = function () {
  console.log("Hello, I'm " + this.name);
};

// Creating instances of Animal
var cat = new Animal("Whiskers");
var dog = new Animal("Buddy");

// Both cat and dog can access the sayHello method from the prototype
cat.sayHello(); // Output: Hello, I'm Whiskers
dog.sayHello(); // Output: Hello, I'm Buddy
```

In this example:

- **cat** and **dog** are instances of the **Animal** constructor function.
- The **sayHello** method is defined on the **Animal.prototype**, making it accessible to both **cat** and **dog**.

This is the fundamental mechanism of prototypal inheritance in JavaScript. It allows objects to share and inherit properties and methods from their prototypes, creating a flexible and efficient way to structure and extend code.


---

Original Source: https://www.mindstick.com/forum/157807/how-does-prototypal-inheritance-work-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
