Q106: Prototypes and the Prototype Chain
What Interviewers Want To Evaluate
This question checks whether you understand JavaScript object inheritance at runtime. Interviewers want the difference between own properties and inherited properties, how lookup walks the prototype chain, how constructor functions connect to prototypes, and why prototype behavior is different from class-based inheritance in languages like Java or C#.
Senior answers should connect prototypes to performance, debugging, built-in objects, class syntax, and safe object design.
Short Interview Answer
Every JavaScript object can have an internal prototype reference. When JavaScript cannot find a property directly on an object, it looks at the object's prototype, then that prototype's prototype, and continues until it reaches null. This lookup path is the prototype chain. Constructor functions and classes place shared methods on a prototype so instances can reuse behavior without copying every method onto every object.
Detailed Interview Answer
JavaScript uses prototype-based inheritance. Objects inherit from other objects.
The simplest mental model is:
object -> prototype -> prototype -> null
Property access first checks the object itself. If the property is not present, JavaScript follows the prototype reference.
const user = { name: "Krishna" };
console.log(user.toString);
toString is not an own property of user, but it exists through Object.prototype.
Own Properties
Own properties live directly on the object.
const user = {
name: "Asha",
};
console.log(Object.hasOwn(user, "name")); // true
Inherited properties are available through lookup, but they are not owned by the object.
console.log(Object.hasOwn(user, "toString")); // false
Prototype Lookup
Prototype lookup is read-time behavior. JavaScript does not copy inherited properties into the object.
const parent = {
role: "admin",
};
const child = Object.create(parent);
child.name = "Meera";
console.log(child.name); // own property
console.log(child.role); // inherited property
If child.role is requested, JavaScript checks child, does not find role, then checks parent.
Shadowing
If an object has an own property with the same name as a prototype property, the own property wins.
child.role = "editor";
console.log(child.role); // "editor"
console.log(parent.role); // "admin"
This is called shadowing. The prototype value still exists; it is simply hidden by the own property during lookup.
Constructor Functions
Before class, constructor functions were commonly used to create objects with shared prototype methods.
function User(name) {
this.name = name;
}
User.prototype.sayHello = function sayHello() {
return `Hello ${this.name}`;
};
const user = new User("Asha");
console.log(user.sayHello());
The name property is stored on each instance. The sayHello method is shared through User.prototype.
The new Operator
When new User("Asha") runs, JavaScript roughly does this:
create a new object
link it to User.prototype
call User with this set to the new object
return the object unless constructor returns another object
This is why instances can access methods placed on User.prototype.
class Syntax
JavaScript class syntax is mostly syntax over prototypes.
class Account {
constructor(owner) {
this.owner = owner;
}
label() {
return `Account: ${this.owner}`;
}
}
label is placed on Account.prototype, not copied into every instance.
Prototype Chain Diagram
Built-In Prototypes
Arrays, functions, dates, maps, and promises all use prototype chains.
const items = [];
console.log(items.map === Array.prototype.map); // true
The array instance does not own map; it inherits map from Array.prototype.
Common Mistakes
A common mistake is saying prototypes copy properties into child objects. They do not.
Another mistake is using obj.__proto__ as a normal API. Prefer Object.getPrototypeOf, Object.setPrototypeOf, and Object.create.
Security and Safety
Prototype pollution happens when unsafe code allows user-controlled input to modify object prototypes or special keys like __proto__.
This can change behavior across many objects and create security issues.
Defensive patterns include:
validate object keys
avoid merging untrusted objects blindly
use Object.create(null) for dictionary objects
prefer Map for arbitrary keys
Performance Considerations
Modern engines optimize stable object shapes and prototype chains. Frequently changing prototypes with Object.setPrototypeOf can make optimization harder.
In production code, choose simple object construction patterns and avoid mutating prototypes dynamically.
Code or Design Example
const permissions = {
canRead: true,
};
const admin = Object.create(permissions);
admin.canDelete = true;
console.log(admin.canRead); // inherited
console.log(admin.canDelete); // own
This shows behavior sharing through a prototype, but for application permissions a plain explicit model is usually clearer.
Debugging Workflow
When a property behaves unexpectedly:
check Object.hasOwn
inspect Object.getPrototypeOf
look for shadowed properties
look for class or constructor prototype methods
check whether a merge changed the object
check whether a dictionary should be a Map
Interview Framing
Start with own property lookup, then explain the prototype chain. Add a constructor or class example to prove you understand how methods are shared.
Revision Notes
Prototypes are runtime object links. Property lookup walks the chain until it finds a property or reaches null.
Key Takeaways
JavaScript inheritance is prototype-based. Classes are friendlier syntax, but the underlying method sharing still uses prototypes.
Follow-Up Questions
- What is an own property?
- What is an inherited property?
- What happens when a property is not found on an object?
- How does
newconnect an instance to a prototype? - Are class methods copied to every instance?
- What is property shadowing?
- Why can changing prototypes hurt performance?
- What is prototype pollution?
- When would you use
Object.create(null)? - How is a prototype chain different from lexical scope?
How I Would Answer This In A Real Interview
I would say that JavaScript first checks own properties and then walks the prototype chain. I would show how constructor functions or classes share methods through the prototype, then mention shadowing, built-in prototypes, and why dynamically mutating prototypes is usually avoided.