JavaScript Tutorial in Hindi #23 | Prototypes & Prototype Chain (2025)
β
1. What is a Prototype (__proto__
) in JavaScript?
In JavaScript, every object has a hidden property called __proto__
that points to its prototype β an object it inherits properties and methods from.
π It forms the base of JavaScript's inheritance system.
β 2. Why Prototypes Are Needed?
- JavaScript uses prototypal inheritance.
- Prototypes reduce memory usage by sharing methods across all instances.
Example with constructor:
β
sayHi()
is shared, not re-created for every object.
β 3. What is the Prototype Chain?
If a property or method is not found on an object, JavaScript looks up the chain using __proto__
, until it reaches null
.
π This lookup path is called the Prototype Chain.
β 4. Simple Example of Prototype Chain
β
JavaScript checks in child
, then parent
, then grandParent
.
β 5. Interview Questions Related to Prototypes
πΈ Q1: What is the difference between __proto__
and prototype
?
__proto__
: Used internally to access the prototype chain of an object.prototype
: A property of functions/constructors that defines what new instances will inherit.
πΈ Q2: How is inheritance handled in JavaScript?
- Through prototypes and the prototype chain.
πΈ Q3: What is the prototype of an object created using {}
?
- Itβs
Object.prototype
.
πΈ Q4: Can you modify an existing objectβs prototype?
- Yes, using
Object.setPrototypeOf()
or__proto__
(not recommended in modern code).
πΈ Q5: What happens if a property is not found in the prototype chain?
- JavaScript returns
undefined
.