Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

What is prototypal inheritance, and how does the prototype chain resolve a property?

Every JavaScript object has a hidden link to another object called its prototype. When you read a property, the engine looks on the object itself; if it is not there, it follows the link to the prototype, then to that object's prototype, and so on until it reaches null. That path is the prototype chain, and a missing property simply returns undefined at the end of it.

The pieces that confuse people:

  • Object.getPrototypeOf(obj) gives you an object's prototype. The legacy __proto__ accessor does the same thing.
  • A function's .prototype property is not that function's prototype. It is the object that will become the prototype of instances created with new.
  • class syntax is sugar over exactly this mechanism. extends sets up the chain; there is no separate class system underneath.

Why it matters practically: methods live on the prototype, so a thousand instances share one copy of each method rather than each carrying its own. It is also why for...in walks inherited enumerable properties and Object.keys does not.

All Javascript interview questions

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as