ES6 Classes and Inheritance
`class` syntax as sugar over the prototype chain — `constructor`/ `extends`/`super`, and how method resolution works when a subclass and parent both define one.
Prototypal Inheritance and Object.createThe `this` Keyword, `call`, `apply`, and `bind`
ES6 Classes and Inheritance
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `class` is syntax sugar over the prototype chain, not a new object model: methods you write inside a class body land on `ClassName.prototype`, exactly where they'd land if you'd assigned them there by hand after a constructor function.
- `extends` wires up the prototype chain between a subclass and its parent automatically — `class Dog extends Animal` makes `Dog.prototype`'s prototype `Animal.prototype` — the same link `Object.create` builds manually, just set up for you.
- A subclass constructor must call `super(...)` before it touches `this` at all; that call runs the parent's constructor first, which is what actually allocates and initializes the `this` the subclass constructor goes on to use.
- Calling a method finds the *nearest* version of it along the prototype chain starting from the instance — if a subclass defines its own `describe()`, that one runs instead of the parent's, even when called through a reference typed as the parent; `super.describe()` inside the override is how you deliberately reach the parent's version anyway.
- Unlike function declarations, `class` declarations are not hoisted in a way that lets you use them before their definition runs — the class exists in a \"temporal dead zone\" until its declaration is evaluated, so referencing it earlier in the file throws instead of silently giving you `undefined`.
A cleaner face on something you already know
By the time class arrived in ES6, JavaScript developers had been doing
object-oriented-style programming for years using constructor functions
and manual prototype wiring — it worked, but the syntax for it was
clunky and easy to get subtly wrong. class didn’t replace that
mechanism with something new; it gave the exact same prototype-based
system a syntax that reads the way inheritance looks in languages people
already knew, like Java or C#. That’s the single most important thing to
internalize about class in JavaScript: it is sugar, not a new engine
feature. Everything you learned about the prototype chain and
Object.create in the last lesson is still exactly what’s running — you’re
just not wiring it up by hand anymore.
Where a class’s methods actually live
Write class Dog { bark() { ... } } and it’s tempting to think bark
belongs to each Dog instance individually. It doesn’t. It lands on
Dog.prototype, shared by every instance through the same chain-walking
lookup from the previous lesson — create a thousand dogs and there’s
still exactly one bark function in memory, found by every instance that
asks for it. This is also why instance fields set in the constructor
(this.name = name) feel different from methods: fields go directly on
the instance because they’re genuinely different per dog, while methods
go on the shared prototype because the behavior is identical across
every instance.
extends and super: wiring the chain, automatically
class Dog extends Animal does one very specific thing mechanically: it
sets Dog.prototype’s prototype to Animal.prototype. That’s the same
link you’d build by hand with Object.create(Animal.prototype) on a
plain constructor function — extends just does it for you and adds
some safety rails on top.
super has two different jobs depending on where you use it. Called as a
function — super(...) — inside a subclass constructor, it runs the
parent class’s constructor, which is what actually creates and
initializes this. JavaScript enforces this strictly: a derived
constructor can’t touch this at all before calling super(), because
until that call runs, there’s no this yet to touch. Called as
super.methodName() inside a method body, it means something different —
“skip my own class’s version and call the parent’s directly,” which is
how an overriding method can still build on the behavior it’s replacing
instead of discarding it entirely.
Which method actually runs
When Dog and Animal both define describe(), and you call
new Dog().describe(), JavaScript doesn’t check “which class defined
this instance” and jump straight there — it walks the prototype chain
starting from the instance, exactly like any other property lookup.
Dog.prototype gets checked first, finds its own describe, and stops.
The parent’s version is only reachable from inside the override, via
super.describe(). This is the same lookup mechanism from the
prototype-chain lesson; class hasn’t changed how resolution works, it’s
just given you a tidier way to define what’s at each link in the chain.
The video covers this full arc — class syntax, constructor, extends,
and super — building from plain object-oriented syntax up to inherited,
overridden methods, which is the order this lesson follows too.
Where this fits
Classes assume you already understand the prototype chain
and how this gets bound when a function runs
— a class’s methods are ordinary functions, and they follow the exact
same this rules as any other method call. What’s left to cover is the
part classes added that plain prototypes never had a clean story for:
genuinely private data and members that belong to the class itself rather
than any instance, which is exactly where
private fields and static members
picks up next.
Check yourself
Where does a method written inside a `class Dog { bark() {...} }` body actually end up?
On `Dog.prototype`, not on individual `Dog` instances. Every instance you create with `new Dog()` shares that one `bark` method through the prototype chain, the same as if you'd written `Dog.prototype.bark = function() {...}` yourself on a constructor function.
What does `class Dog extends Animal` actually set up, in terms of the prototype chain?
It sets `Dog.prototype`'s internal prototype to `Animal.prototype`. That's the whole mechanism — any method missing on `Dog.prototype` is now found by walking up to `Animal.prototype`, which is exactly how a `Dog` instance ends up able to call methods it never defined itself.
Why does `super(...)` have to be called before you can use `this` in a subclass constructor?
Because in a derived class, the parent constructor is responsible for actually creating and initializing `this` — the subclass constructor doesn't get a usable `this` until `super()` runs and hands one back. Trying to reference `this` before that call throws a `ReferenceError`, by design.
`Dog` extends `Animal`, and both define a `describe()` method. You call `new Dog().describe()`. Which one runs, and how would the `Dog` version still call the `Animal` one?
`Dog`'s own `describe()` runs — method lookup finds the nearest match starting from the instance, and `Dog.prototype` is checked before `Animal.prototype`. If `Dog`'s `describe()` wants to also run the parent's logic, it calls `super.describe()` explicitly from inside its own method body.
What happens if you try to use a class before its declaration in the file, the way you might with a hoisted function?
It throws a `ReferenceError`. Class declarations aren't usable before they're evaluated — they sit in a temporal dead zone from the top of their scope until the declaration itself runs, unlike `function` declarations, which are fully hoisted and callable before their definition appears in the file.
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.