Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 1 of 10 · Prototypes and Inheritance
Interview question

What is the prototype chain in JavaScript? JavaScript में prototype chain क्या है?

Answer

Every JavaScript object has an internal link to another object called its prototype. When a property or method isn't found on the object itself, JavaScript looks up the chain of prototypes until it finds it or reaches null.

const animal = {
    eats: true,
    walk() {
        console.log('Animal walks');
    }
};

const rabbit = {
    jumps: true,
    __proto__: animal // rabbit's prototype is animal
};

console.log(rabbit.jumps); // true - own property
console.log(rabbit.eats);  // true - found on prototype (animal)
rabbit.walk();              // 'Animal walks' - method found via prototype chain

// The chain can go multiple levels deep
const longEar = {
    earsAreLong: true,
    __proto__: rabbit
};
console.log(longEar.eats); // true - looked up: longEar -> rabbit -> animal

// The chain ends at Object.prototype, then null
console.log(animal.__proto__ === Object.prototype); // true
console.log(Object.prototype.__proto__); // null - end of the chain

// hasOwnProperty checks only the object's OWN properties, not inherited ones
console.log(rabbit.hasOwnProperty('jumps')); // true
console.log(rabbit.hasOwnProperty('eats'));  // false - inherited, not own

// 'in' operator checks the whole chain
console.log('eats' in rabbit); // true
Lookup flow: Access property → Check own properties → Not found? Check prototype's own properties → Not found? Check prototype's prototype → ... → Reach null → return undefined

हर JavaScript object का दूसरे object से internal link होता है जिसे उसका prototype कहते हैं। जब property या method object पर नहीं मिलती, JavaScript prototypes की chain में ऊपर देखता है जब तक मिल न जाए या null तक पहुंच न जाए।

const animal = {
    eats: true,
    walk() {
        console.log('Animal walks');
    }
};

const rabbit = {
    jumps: true,
    __proto__: animal
};

console.log(rabbit.jumps); // true - own property
console.log(rabbit.eats);  // true - prototype से मिला
rabbit.walk();              // 'Animal walks'

// Chain कई levels तक जा सकती है
const longEar = {
    earsAreLong: true,
    __proto__: rabbit
};
console.log(longEar.eats); // true

// Chain Object.prototype पर खत्म होती है, फिर null
console.log(animal.__proto__ === Object.prototype); // true
console.log(Object.prototype.__proto__); // null

// hasOwnProperty सिर्फ own properties check करता है
console.log(rabbit.hasOwnProperty('jumps')); // true
console.log(rabbit.hasOwnProperty('eats'));  // false

// 'in' operator पूरी chain check करता है
console.log('eats' in rabbit); // true
Lookup flow: Property access → own properties check → prototype की own properties check → ... → null तक पहुंचना → undefined return

Was this answer clear?