🧩 Prototypes & Prototype Chain in JavaScript (OOP)
Last Updated: 25th October 2025
- Every object in JavaScript is connected to another object called its prototype.
- The prototype acts as a blueprint or parent that shares properties and methods with its child objects.
- Each JS object has a hidden property called
[[Prototype]](accessible by__proto__). - It points to another object (the prototype).
- If JS doesn’t find a property/method in the object, it looks for it in the prototype.
Hinglish Tip 🗣: Prototype ko ek “parent object” samjho — jisse child object features (properties/methods) inherit karta hai.
Example:
const person = {
greet() {
console.log("Hello!");
},
};
const student = {
name: "Sachin",
};
// Linking student → person
student.__proto__ = person;
student.greet(); // Output: Hello!
👉 Here, student doesn’t have greet(), so JS looks in person (its prototype).
📚 Prototype Chain
The process of looking up a property/method in parent prototypes is called the Prototype Chain.
It continues until:
- The property is found, OR
- It reaches the top of the chain → Object.prototype (which has methods like toString(), hasOwnProperty(), etc.)
- JS searches → child → parent → grandParent → Object.prototype
Example:
const grandParent = {
sayHi() {
console.log("Hi from GrandParent!");
},
};
const parent = Object.create(grandParent);
const child = Object.create(parent);
child.sayHi(); // Output: Hi from GrandParent!
Hinglish Tip 🗣: Chain me upar tak search hota hai jab tak mil nahi jata — isiliye ise “prototype chain” kehte hain.
💡 Quick Practice
- Create a prototype object animal with eat() method and make dog inherit it.
- Use Object.create() to chain 3 levels (child → parent → grandparent).
- Add a method to constructor’s prototype and access it from two instances.