Prototypes & Prototype Chain Kya Hota Hai – What is JavaScript Prototypes & Prototype Chain

JavaScript mein sab kuch object hota hai?

JavaScript mein primitive types (jaise: number, string, boolean) ke alawa har cheez ek object hoti hai ya object ke jaisa behave karti hai. Jab hum kisi object par koi method call karte hain, toh JavaScript use us object ke andar, aur agar wahan na mile toh uske prototype mein dhoondhti hai.

Prototype Kya Hai? (What is a Prototype?)

Definition:

Prototype ek JavaScript ka feature hai jo objects ke beech inheritance ko enable karta hai. Har JavaScript object ke paas ek hidden property hoti hai jise [[Prototype]] kehte hain, jo kisi aur object ko reference karti hai. Isse ek object dusre object ke properties aur methods access kar sakta hai.

Jab kisi property ki talash hoti hai, to JavaScript pehle object mein dekhta hai, agar nahi milti to prototype chain mein upar jaake search karta hai.

Example:

let person = {
  name: "Ravi",
  greet: function () {
    console.log("Hello!");
  }
};

console.log(person.toString()); // [object Object]

Yahan toString() method person object mein nahi likha, phir bhi kaam kar raha hai — kyun? Kyunki person ka prototype Object.prototype hai, jisme toString() method defined hai.

Prototype Chain Kya Hai? (What is Prototype Chain?)

JavaScript mein Prototype Chain ek mechanism hai jiske through objects properties aur methods inherit karte hain. Jab hum kisi object ki property access karte hain, to JavaScript sabse pehle us object mein us property ko dhundhta hai. Agar wahan nahi milti, to woh uske prototype mein search karta hai. Ye process tab tak chalta hai jab tak null prototype tak nahi pahunch jaata – isey hi Prototype Chain kehte hain.

Ye concept JavaScript ke object-oriented nature ko support karta hai, jahan objects dusre objects se behavior inherit karte hain. Har object ka ek internal property hota hai [[Prototype]] ya __proto__, jo uske parent object ki taraf point karta hai.

Chain kuch is tarah hoti hai:

customObject → Object.prototype → null

Example:

let car = {
  brand: "Tata"
};

console.log(car.toString()); // [object Object]

toString() car object mein nahi hai → JavaScript car.__proto__ (yaani Object.prototype) mein jati hai → wahan se milta hai.

Prototype Se Inheritance Kaise Hota Hai?

Prototype se inheritance JavaScript mein ek aisa concept hai jisme ek object doosre object ke properties aur methods ko access kar sakta hai. JavaScript mein har object ke paas ek internal link hota hai jise [[Prototype]] ya __proto__ kaha jata hai. Jab hum kisi property ko access karte hain aur wo current object mein nahi milti, to JavaScript prototype chain ke through us property ko uske prototype object mein dhoondta hai. Is tarah, ek object apne prototype se inherit karta hai. Hum constructor functions ya Object.create() ka use karke prototype inheritance implement kar sakte hain. Ye concept code reusability aur memory optimization ke liye bahut useful hota hai, especially jab multiple objects similar behavior share karte hain.

Example:

let animal = {
  eats: true
};

let dog = Object.create(animal);

console.log(dog.eats); // true
  • dog object mein eats property nahi hai.
  • Lekin dog ka prototype animal hai, jisme eats: true likha hai.
  • Isliye output true milta hai.

Constructor Functions and Prototype

Constructor functions JavaScript mein ek tarah ka function hota hai jo naye objects banane ke liye use kiya jata hai. Jab hum kisi function ko new keyword ke saath call karte hain, toh woh constructor ban jata hai. Prototype ek built-in property hoti hai jiske through hum objects mein common methods ya properties share kar sakte hain. Isse memory efficient programming hoti hai, kyunki har object ke liye alag se method define nahi karna padta.

Example:

function Person(name) {
  this.name = name;
}

Person.prototype.sayHello = function () {
  console.log("Hi, I’m " + this.name);
};

let user1 = new Person("Amit");
user1.sayHello(); // Hi, I’m Amit

user1 ka prototype hai Person.prototype jahan sayHello() define hai.

Prototype Chain Example:

function Animal() {
  this.legs = 4;
}
Animal.prototype.speak = function () {
  return "Animal sound";
};

function Dog() {
  this.bark = true;
}

Dog.prototype = new Animal(); // Inheritance
let myDog = new Dog();

console.log(myDog.legs); // 4
console.log(myDog.speak()); // "Animal sound"

Prototype Chain Hierarchy:

myDog → Dog.prototype → Animal.prototype → Object.prototype → null

Important Points:

  • JavaScript mein objects dynamically properties inherit karte hain.
  • Functions ke paas prototype hota hai, objects ke paas __proto__ hota hai.
  • Prototype chain ka end Object.prototype hota hai jiska prototype null hota hai.
  • Agar aap koi method define karte ho prototype mein, to wo sab instances ke liye available hoti hai (memory efficient).

Memory Optimization using Prototypes

Agar aap har object ke andar methods define karoge, to memory zyada lagegi.

Wrong Way (waste of memory):

function User(name) {
  this.name = name;
  this.sayHi = function () {
    console.log("Hi, " + this.name);
  };
}

Right Way (efficient):

function User(name) {
  this.name = name;
}
User.prototype.sayHi = function () {
  console.log("Hi, " + this.name);
};

Override Prototype Method

Agar aap same naam ki method object mein define kar dete ho, to wo prototype wali ko override kar deti hai.

let user = {
  toString: function () {
    return "Custom User";
  }
};

console.log(user.toString()); // "Custom User"

Final Summary

  • Prototype ek object hai jise JavaScript use karti hai inheritance ke liye.
  • Agar kisi object mein property/method nahi milti, to JavaScript uske prototype mein dhoondti hai.
  • Prototype Chain ka matlab hai ek chain jahan JavaScript property ko top level tak trace karti hai.
  • prototype (constructor functions ke liye), __proto__ (har object ke paas hoti hai), aur [[Prototype]] (internal mechanism) teen important parts hain.

Quiz: Test Your Knowledge on JavaScript Prototypes & Prototype Chain

Bonus: Practical Application!

Aaj hi JavaScript ke prototypes aur prototype chain ka istemal karke apna code aur bhi efficient banayein!

JavaScript Prototypes & Prototype Chain ko sahi tareeke se samajhne ke liye different concepts jaise object inheritance, __proto__, prototype property, constructor functions, aur method chaining ka upayog karein — aur dekhein kaise JavaScript ke objects ek doosre se connected hote hain through prototype chain.

Leave a Reply