Intermediate LevelJavaScript & TypeScript
What is JavaScript prototype inheritance and the prototype chain?
❓ Interview Prompt: Explain __proto__, prototype property, object delegation, and property lookup along the prototype chain.
JavaScript objects inherit properties directly from other objects via a delegation mechanism called the Prototype Chain.
Key Mechanisms:
1
Every object has an internal hidden link (
[[Prototype]], exposed via Object.getPrototypeOf()).2
When accessing a property on an object:
•
JS checks if the property exists on the instance object itself.
•
If missing, JS looks up the prototype object, continuing up the chain until reaching
Object.prototype and finally null.3
Constructor functions have a
.prototype property that becomes the [[Prototype]] for instances created with new.Code Implementation Example
javascript
function Animal(name) {
this.name = name;
}
// Shared method on prototype (saves memory)
Animal.prototype.makeSound = function() {
return `${this.name} makes a noise.`;
};
const dog = new Animal('Rex');
console.log(dog.hasOwnProperty('name')); // true (own property)
console.log(dog.hasOwnProperty('makeSound')); // false (inherited from prototype!)
console.log(Object.getPrototypeOf(dog) === Animal.prototype); // true💡Constructor function prototype assignment and property lookup along the prototype chain.
🎯 Interview Key Takeaway
Objects inherit methods via prototype delegation. Shared methods belong on constructor.prototype to avoid duplication.
#Prototypes#Inheritance#OOP#Intermediate
Back to all JavaScript & TypeScript Questions→