← Lessons

quiz vs the machine

Gold1350

Frontend

Prototypes and Inheritance

How objects share behavior through a chain of links.

5 min read · core · beat Gold to climb

The prototype link

Every JavaScript object has a hidden link to another object called its prototype. When you read a property the engine misses, it follows this link to look there instead.

The prototype chain

These links form a chain. A lookup walks from the object up through each prototype until it finds the property or reaches the end, which is null. This chain is how objects inherit methods without copying them.

Where methods live

For a class or constructor, methods are stored once on the prototype object, and every instance links to it. Calling a shared method finds it up the chain, so all instances reuse one function rather than each holding a copy.

Own versus inherited

  • An own property sits directly on the object.
  • An inherited property is found further up the chain.
  • A check for an own property ignores the chain, which matters when iterating keys.

Classes are sugar

The class keyword is a cleaner syntax over this same machinery. Extends sets up the prototype link between child and parent, and super reaches the parent method up the chain.

Key idea

Objects inherit by linking to a prototype; lookups walk the chain until they find the property or hit null.

Check yourself

Answer to earn rating on the learn ladder.

1. What happens when a property is not found on an object itself?

2. Where are a class instance methods stored?