JavaScript · The language round
How does prototypal inheritance work?
Short answer
Objects inherit through a prototype chain. A property lookup checks the object, then its prototype, then the prototype's prototype, until it finds the property or reaches null.
JavaScript does not copy members from a parent class into a child. Each object has an internal link to another object, its prototype. Property access follows that link at lookup time, so inherited behaviour stays live.
const animal = {
eats: true,
walk() {
console.log("walking");
},
};
const rabbit = Object.create(animal);
rabbit.jumps = true;
console.log(rabbit.eats); // true, inherited
console.log(Object.hasOwn(rabbit, "eats")); // false
rabbit.walk(); // "walking"rabbit owns jumps. eats and walk are found on its prototype.Prototype vs __proto__
Object.getPrototypeOf(obj)reads the prototype safelyObject.setPrototypeOf(obj, proto)changes it, but is slow and discouraged in hot codeobj.__proto__is a legacy accessor for the same linkFn.prototypeis the object that instances created withnew Fn()will use as their prototype
Classes are syntax over the same mechanism. extends sets up the prototype chain, and super looks up the parent prototype.
class Animal {
speak() {
return "sound";
}
}
class Dog extends Animal {
speak() {
return super.speak() + ": woof";
}
}
console.log(new Dog().speak()); // "sound: woof"Why it matters
- Shared methods live once on the prototype instead of on every instance
instanceofwalks the prototype chain- Changing a prototype method affects existing instances
- Mutating built-in prototypes such as
Array.prototypeis risky and usually avoided
Object.create(null) makes an object with no prototype. It has no inherited methods such as toString, which is useful for safe dictionaries.Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between `prototype` and `__proto__`?
- 2How does `instanceof` work?
- 3When would you use `Object.create(null)`?
- 4Why should you avoid mutating built-in prototypes?
- 5How do ES classes relate to prototypes?
Say it in the interview
Every object has an internal prototype link. When I read a property, JavaScript checks the object first, then follows the link to its prototype, then that object's prototype, until it finds the property or reaches null. Classes use the same chain under the hood. Shared methods usually live on the prototype so instances do not each carry a copy. `Object.create` lets me set the prototype explicitly, and `prototype` on a constructor is the object that new instances will inherit from.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live JavaScript mock interview and get scored feedback on depth, clarity and confidence.