Skip to the solution
MediumFrontend8 min read

Explain prototypal inheritance.

557 views

The problem

Question presented to candidate: "Explain what prototypal inheritance actually is in JavaScript, and how the mechanism works under the hood."

What a strong answer should cover:

  • Every JavaScript object has a hidden internal link, the [[Prototype]] slot, pointing to another object (or to null).
  • Property lookup walks this link: if a property is not found directly on an object, the engine automatically checks the object's prototype, then that prototype's own prototype, and so on, until it finds the property or reaches null.
  • Object.create(proto) is the most direct way to create an object with a chosen prototype, with no constructor function involved at all.
  • Ctor.prototype (a plain object property that lives on a function) and instance.proto (the actual internal link on an instance) are two distinct but related things, and mixing them up is a very common source of confusion.
  • Because lookup happens live at call time rather than by copying, patching a shared prototype after instances already exist changes the behavior of every existing instance -- inheritance is genuinely dynamic, not a one-time copy.

Clarifying questions expected:

  • "Should I also contrast this with classical, class-based inheritance, or focus purely on how the JavaScript mechanism itself works?" (the comparison has its own dedicated question)

Code / implementation expected: Yes -- a runnable snippet showing Object.create-based delegation and a live patch to a shared prototype changing an existing instance's behavior.

  • prototypes
  • inheritance
  • objects
  • oop

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

8 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptObject.create delegation, prototype vs __proto__, and live (non-copied) inheritance (run directly)

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 83 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What are closures in JavaScript?

Continue