Question presented to candidate: "How does the way inheritance works in JavaScript actually differ from classical, class-based inheritance in a language like Java or C++?"
What a strong answer should cover:
- Classical inheritance is a blueprint-and-instance model: a class is a fixed template, checked and fixed at compile time, and objects are stamped out from it.
- Prototypal inheritance is an object-to-object delegation model: there are no blueprints -- objects link directly to other live objects, and lookup happens dynamically at runtime.
- Delegation vs copying: a prototypal object does not receive a private copy of its prototype's methods -- it looks them up live, every time, by following a real link.
- Dynamic vs static: a JavaScript prototype can be patched or extended after instances already exist, and every existing instance picks up the change immediately -- a compiled Java class cannot be altered at runtime.
- Even JavaScript's class keyword does not introduce true classical inheritance underneath -- it is syntax sugar over the exact same prototype chain, and a class's methods remain a live, mutable object (Ctor.prototype) that can still be patched after the fact.
Clarifying questions expected:
- "Should I focus on the conceptual difference, or also cover exactly how the JavaScript prototype mechanism itself works?" (the mechanism has its own dedicated question)
Code / implementation expected: Yes -- a runnable snippet proving a class's prototype is still live and patchable after declaration, unlike a compiled classical class.