Prototypal Inheritance and Object.create
How JS objects share behavior without classes — the prototype chain, `Object.create`, `__proto__` vs `.prototype`, and how property lookup walks the chain.
Prototypal Inheritance and Object.create
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Every JavaScript object has an internal link to another object, its prototype, and reading a property that isn't found directly on the object makes the engine check the prototype next, then its prototype, and so on — this chain of fallbacks is prototypal inheritance.
- `Object.create(proto)` makes that link explicit: it returns a brand-new object whose prototype is exactly the object you passed in, with no constructor function or `new` involved at all.
- `.prototype` and `__proto__` are not the same thing wearing two names: `.prototype` is a property that only exists on functions (it's the object that will become the prototype of anything built with `new ThatFunction()`), while `__proto__` is the actual link on an instance pointing to its prototype.
- Property lookup only walks the chain on reads. Writing to `obj.key = value` always creates or updates the property directly on `obj`, even if a property with that name already exists somewhere up the chain — the write never "reaches through" to the ancestor.
- The chain has an end: it stops at `Object.prototype`, whose own prototype is `null`. `Object.create(null)` skips even that, producing an object with no prototype at all — useful when you want a plain lookup table with zero inherited properties to collide with your keys.
Sharing behavior without copying it
Say you’ve got a hundred objects that all need a describe() method that
does the same thing. You could paste the same function onto every one of
them, but that wastes memory and means fixing a bug in a hundred places.
JavaScript’s answer predates class entirely: instead of copying
behavior onto every object, let objects share it by pointing at a common
object that holds it. That shared object is called a prototype, and the
mechanism that lets one object “borrow” properties from another this way
is prototypal inheritance — the thing class syntax later wraps in more
familiar-looking clothes, but never replaces.
The chain, and how lookup actually walks it
Every object carries an internal link to another object: its prototype.
When you read obj.someProperty, JavaScript doesn’t just check obj and
give up if it’s not there. It checks obj first, and if the property is
missing, it checks obj’s prototype next, then that object’s prototype,
and so on — a chain of fallbacks that only ends when a property is found
or there’s no prototype left to check. This chain terminates at
Object.prototype, the one object almost everything inherits from
eventually, whose own prototype is null — JavaScript’s signal for “the
chain stops here.”
This walk only happens on reads. Assigning obj.key = value never
reaches up the chain looking for an existing key to overwrite — it
always creates or updates a property directly on obj, even when a
property with the same name already exists on something further up. If
that new own property has the same name as an inherited one, it
shadows the inherited version: future reads find the own property
immediately and stop, without ever consulting the prototype again for
that name.
Building the link directly with Object.create
Object.create(proto) is the most direct way to see this mechanism at
work, with nothing else layered on top. It returns a brand-new, empty
object whose prototype is set to exactly the object you pass in — no
constructor function runs, no new keyword is involved. That makes it
the cleanest tool for understanding what a prototype link is, separate
from everything new and constructor functions also do at the same time.
It’s also the only built-in way to create an object with no prototype
at all: Object.create(null) gives you an object that doesn’t even
inherit from Object.prototype, which is handy for a lookup table you
don’t want polluted with inherited properties like toString.
.prototype and __proto__ are not the same word twice
This is the single most common point of confusion in this whole topic,
and it’s worth being precise about. .prototype is a property that lives
on functions — it’s the object that will become the prototype of any
instance you create by calling that function with new. __proto__, by
contrast, lives on instances — it’s the actual internal link pointing
to whatever object is currently that instance’s prototype. A function has
a .prototype because it’s a blueprint for instances that haven’t been
made yet; an instance has a __proto__ because it’s already linked to
whatever .prototype was in effect when new created it. The video
walks through both sides of that distinction with real objects in the
console, which is the fastest way to stop mixing them up.
Where this fits
This lesson is the mechanism that ES6 classes and inheritance
is sugar over — class Dog extends Animal is doing exactly what you just
saw, wiring up a prototype chain between Dog.prototype and
Animal.prototype, just with cleaner syntax and a few safety rails
class adds on top. If the prototype chain and Object.create make
sense now, extends and super in the next lesson will read as a
shorthand for something you already understand, not a new concept to
memorize from scratch.
Check yourself
You call `obj.greet()` and `obj` itself has no `greet` property. What does JavaScript actually do before it throws a `TypeError`?
It checks `obj`'s prototype for a `greet` property. If that's not there either, it checks the prototype's prototype, and keeps going up the chain until it either finds `greet` somewhere or reaches the end of the chain (`Object.prototype`'s prototype, which is `null`) with nothing found — only then does it throw.
What does `Object.create(proto)` actually return, and how is that different from calling `new SomeConstructor()`?
It returns a new, empty object whose prototype is set to exactly the `proto` object you passed in — no constructor runs, no `this` gets initialized by any function body. `new SomeConstructor()` also creates a new object linked to a prototype (`SomeConstructor.prototype`), but it additionally runs the constructor function body with `this` bound to that new object.
Is `.prototype` a property you'd find on every object, or only on certain things?
Only on functions (and classes, which are functions under the hood). `.prototype` is the object that becomes the prototype of any instance created by calling that function with `new`. A plain object instance doesn't have a `.prototype` property — it has `__proto__`, which points *to* its prototype rather than defining one for future instances.
If `child` inherits a property `count` from its prototype, what happens when you run `child.count = 5`?
It creates a brand-new `count` property directly on `child` itself, set to `5`. The prototype's `count` is untouched. From then on, reading `child.count` finds the new own property immediately and never has to walk the chain — this is called shadowing.
Why would you ever want `Object.create(null)` instead of a normal `{}`?
A normal `{}` inherits from `Object.prototype`, which means it already has properties like `toString` and `hasOwnProperty` before you add anything of your own. If you're using an object purely as a key-value lookup table with keys you don't fully control, `Object.create(null)` gives you an object with no inherited properties at all, so a key like `"toString"` behaves like any other string key instead of colliding with something already there.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.