Learn JavaScript
❯ classes_oop.extends

Inheritance with extends in JavaScript

$concept

extends builds subclasses inheriting methods and state: class Dog extends Animal gets Animal's interface plus its own additions — the is-a relationship (every Dog works where an Animal is expected). Prototype chains implement it: Dog.prototype delegates to Animal.prototype, so method lookup walks upward.

Prefer composition over deep hierarchies (has-a via fields beats is-a chains past two levels), and remember extends accepts expressions — class X extends Mixin(Base) enables mixin composition patterns.

$how it works
1

is-a modeling

class Dog extends Animal — Dog instances answer Animal messages plus Dog's own.

2

Prototype chains implement

Lookup walks Dog.prototype → Animal.prototype → Object.prototype — delegation, not copying.

3

Expressions extend too

class X extends factory() — dynamic base classes power mixin patterns (Timestamped(Base)).

4

Favor shallow trees

Two levels read well; deeper hierarchies tangle — compose behaviors instead of nesting them.

$example
class Animal {
constructor(name) {
this.name = name;
}
}
class Dog extends Animal {
bark() {
return this.name + " woofs";
}
}
console.log(new Dog("Rex").bark());
Output
Rex woofs
1Dog inherits Animal's constructor path (implicit default calling super with same args).
2name initializes through the inherited chain; bark adds Dog-specific behavior.
3Rex woofs — inherited state plus specialized method in one instance.
$use cases
Specializing general concepts (Error subtypes, component variants)
Framework base classes (React.Component) extended by user code
Mixin factories composing behaviors without deep trees
Choosing composition once hierarchies exceed two levels

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.