Objects, prototypes and classes
Lookup chains, new, and classes as syntax over prototypes.
Objects inherit through a chain: a missing key is looked up on the
prototype, then its prototype, until null. Interviewers ask this because
"shared methods, own data" explains classes, instanceof and half of all
framework code.
Real-life situation
A device settings page builds phones and laptops from one Device
constructor. The info method lives once on the prototype, so a thousand
devices share one function while each keeps its own name. A Laptop
subclass reuses the wiring through extends without copying anything.
Visual
Try it
function Device(name) {
this.name = name;
}
Device.prototype.info = function () {
return "device:" + this.name;
};
const phone = new Device("pixel");
console.log(phone.info());
console.log(phone.hasOwnProperty("info"));
class Laptop extends Device {
constructor(name, ram) {
super(name);
this.ram = ram;
}
}
const notebook = new Laptop("thinkpad", 16);
console.log(notebook.info());
console.log(notebook instanceof Device);
console.log(notebook instanceof Laptop);Expected output
device:pixel false device:thinkpad true true
Try adding phone.info = "mine" and watch hasOwnProperty flip while the
prototype stays shared. Then check notebook.hasOwnProperty("ram").
Sketch the three-box chain for your example on paper once — lookup
questions get easy when you can see the links.
When classes help, when they hide
Classes help for instance hierarchies with shared behavior (devices, views, errors). They hide when a factory plus closures would do — prefer the simpler shape until inheritance earns its keep.
Shadowing the prototype — guess first, then reveal.
const base = { role: "user" };
const admin = Object.create(base);
admin.role = "admin";
console.log(admin.role);
console.log(base.role);Interview questions
How does property lookup work on an object?basic
JavaScript checks the object itself first; if the key is missing it walks up the prototype chain — the object's prototype, then that prototype's prototype — until it finds the key or reaches null (result: undefined). Methods feel "shared" because they live on the prototype, not on each instance. Shadowing happens when the object itself defines the same key and stops the walk early.
Interviewers also ask
- Where do array methods like map actually live?
What is the prototype chain?intermediate
The linked list of objects JavaScript consults for missing properties: instance → Constructor.prototype → Object.prototype → null. new wires the link at creation; class extends wires a longer one. Lookup is one-directional and read-only up the chain — assignment always lands on the object itself. Draw it as three boxes with arrows and most prototype questions answer themselves.
Interviewers also ask
- What sits at the end of every chain?
What does the new keyword actually do?basic
Four things: it makes a fresh object, links it to the constructor's prototype, runs the constructor with this set to that object, and returns the object (unless the constructor returns its own). The linking step is the whole point — it is what gives the instance its shared methods. Everything class does is this procedure in cleaner clothes.
Interviewers also ask
- What if the constructor returns an object explicitly?
Are classes real, or syntax sugar over prototypes?intermediate
Mostly sugar with a few extras. class Cart { total() {} } creates the same constructor-plus-prototype wiring you would write by hand, so instanceof and the chain behave identically. The extras: classes throw when called without new, methods are non-enumerable, and extends/super wire inheritance with less boilerplate. Know both layers — write class, debug in prototypes.
Interviewers also ask
- What does extends compile down to, conceptually?
How do you create an object with a specific prototype?basic
Object.create(proto) makes an object whose prototype is exactly proto — no constructor involved. It is the cleanest way to set up delegation ("fall back to these defaults") and the textbook answer to "inherit without new". For the common case, object literals (prototype: Object.prototype) and classes cover everything; reach for Object.create when the prototype itself is the point.
Interviewers also ask
- When is Object.create better than a class?
What is the difference between __proto__, prototype and [[Prototype]]?intermediate
[[Prototype]] is the spec's name for the hidden link every object has. .prototype is a regular property that exists only on functions, holding the object future instances will link to. __proto__ is the legacy accessor exposing an instance's link — readable everywhere, deprecated for setting (use Object.getPrototypeOf / Object.create instead). Interviewers ask to check you don't set __proto__ in new code.
Interviewers also ask
- How do you read an object's prototype today?
How do object spread and Object.assign copy?basic
Both shallow-copy own enumerable properties into a target: later sources overwrite earlier ones, getters run once and become plain values, and nested objects stay shared. Neither copies prototypes — the result's prototype is Object.prototype. Use them for flat merges and defaults; for nested state, spread every edited level or structuredClone.
Interviewers also ask
- What happens to getters during a spread?
When does instanceof lie to you?intermediate
instanceof walks the prototype chain, so it breaks across realms (an array from an iframe fails instanceof Array here) and after manual prototype surgery. It also returns true for parent classes, which surprises people expecting exact-type checks. Prefer Array.isArray for arrays and capability checks ("does it have .then?") over ancestry tests in library code.
Interviewers also ask
- How do you check an array across iframes?
What happens when you assign to a property the object inherits?intermediate
The assignment creates an OWN property on the object, shadowing the inherited one — the prototype is never modified. That is why one instance can override a shared default without affecting siblings. Deleting the own property reveals the inherited value again, a trick that occasionally appears in puzzles and almost never in production.
Interviewers also ask
- Does assignment ever write through to the prototype?
How would you explain classes to someone who only knows objects?basic
A class is a factory plus a shared backpack: the constructor builds each instance's own properties, and every method lives once on the prototype backpack all instances share. extends just chains two backpacks. If you can point at which properties are own (per-instance data) versus shared (methods), you understand classes well enough for any interview.
Key takeaways
- Missing keys walk the prototype chain to
null. newmakes an object, links it, runs the constructor withthis, returns it.- Classes are constructors plus prototypes with safer defaults.
References
Last verified: 2026-10-12.
Runner helpers
sleep(ms)waits;fakeFetch(url)returns deterministic sample data. Console code runs in a worker with a 5-second limit.