Interview question
How can you check an object's prototype chain and determine what an object inherits from? किसी object की prototype chain कैसे check करें और वो क्या inherit करता है जानें?
Answer
JavaScript provides several methods to inspect the prototype chain, check inheritance relationships, and list inherited vs own properties.
class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound`; }
}
class Dog extends Animal {
bark() { return 'Woof'; }
}
const rex = new Dog('Rex');
// instanceof - checks if prototype exists anywhere in the chain
console.log(rex instanceof Dog); // true
console.log(rex instanceof Animal); // true - inherited through the chain
console.log(rex instanceof Object); // true - everything inherits from Object eventually
// Object.getPrototypeOf - walks up the chain manually
let proto = Object.getPrototypeOf(rex);
console.log(proto === Dog.prototype); // true
proto = Object.getPrototypeOf(proto);
console.log(proto === Animal.prototype); // true
proto = Object.getPrototypeOf(proto);
console.log(proto === Object.prototype); // true
// isPrototypeOf - checks if an object is anywhere in another's chain
console.log(Animal.prototype.isPrototypeOf(rex)); // true
// hasOwnProperty vs 'in' - distinguishing own vs inherited
console.log(rex.hasOwnProperty('name')); // true - set in constructor
console.log(rex.hasOwnProperty('speak')); // false - inherited method
console.log('speak' in rex); // true - 'in' checks the whole chain
// Object.getOwnPropertyNames - lists only OWN properties (not inherited)
console.log(Object.getOwnPropertyNames(rex)); // ['name']
// Listing all methods available via the prototype chain (for debugging)
function getAllMethods(obj) {
let methods = [];
let current = obj;
while (current) {
methods = methods.concat(Object.getOwnPropertyNames(current));
current = Object.getPrototypeOf(current);
}
return [...new Set(methods)];
}
console.log(getAllMethods(rex)); // includes name, bark, speak, constructor, etc.JavaScript prototype chain inspect करने, inheritance relationships check करने, और inherited vs own properties list करने के कई methods देता है।
class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound`; }
}
class Dog extends Animal {
bark() { return 'Woof'; }
}
const rex = new Dog('Rex');
// instanceof - chain में कहीं भी prototype check करता है
console.log(rex instanceof Dog); // true
console.log(rex instanceof Animal); // true
console.log(rex instanceof Object); // true
// Object.getPrototypeOf - chain manually walk करना
let proto = Object.getPrototypeOf(rex);
console.log(proto === Dog.prototype); // true
proto = Object.getPrototypeOf(proto);
console.log(proto === Animal.prototype); // true
// isPrototypeOf
console.log(Animal.prototype.isPrototypeOf(rex)); // true
// hasOwnProperty बनाम 'in'
console.log(rex.hasOwnProperty('name')); // true
console.log(rex.hasOwnProperty('speak')); // false - inherited
console.log('speak' in rex); // true
// सिर्फ own properties की list
console.log(Object.getOwnPropertyNames(rex)); // ['name']
// पूरी chain से methods list करना
function getAllMethods(obj) {
let methods = [];
let current = obj;
while (current) {
methods = methods.concat(Object.getOwnPropertyNames(current));
current = Object.getPrototypeOf(current);
}
return [...new Set(methods)];
}
console.log(getAllMethods(rex));Was this answer clear?