Skip to content
The Prototype Chain
step 1/5

Reading — step 1 of 5

Learn

~1 min readIteration and Generators

JavaScript classes are syntactic sugar for prototype-based inheritance. Understanding the chain is essential for debugging real-world code.

javascript

Under the hood:

  • rex.__proto__ === Dog.prototype
  • Dog.prototype.__proto__ === Animal.prototype
  • Animal.prototype.__proto__ === Object.prototype
  • Object.prototype.__proto__ === null

When rex.speak() is called, JS walks the chain until it finds speak.

Inspect the chain:

javascript

Direct manipulation (avoid in production code, but useful to know):

javascript

Plain objects' prototype chain:

javascript

Object.create(null) — true blank object with no prototype, no toString, no hasOwnProperty. Useful for hashmap-like usage:

javascript

Class fields and methods are added to the prototype (methods) or the instance (fields):

javascript

Performance: lookups walk the chain — deep chains slow down hot code. Most code: don't worry. JIT-compiled hot paths: keep chains shallow.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…