extends
Creates a subclass relationship.
Class inheritance lets one class extend another, but composition is often simpler when behavior does not truly form a hierarchy.
class Widget {
constructor(title) { this.title = title; }
}
class ButtonWidget extends Widget {
click() { return `${this.title} clicked`; }
}
Objects, Prototypes & Classes
A subclass extends a parent class. The subclass inherits methods from the parent and can add or override behavior.
When a subclass has its own constructor, it must call super() before using this. super() runs the parent constructor.
Inheritance is useful for real is-a relationships. If the relationship is only about sharing a small helper, composition or plain functions are often cleaner.
Creates a subclass relationship.
Calls the parent constructor.
A subclass can replace a parent method.
Using smaller objects/functions instead of a class hierarchy.
Examples
class Widget {
constructor(title) {
this.title = title;
}
}
class Panel extends Widget {
renderTitle() {
return this.title;
}
}
class FormatHelpers {
format(value) { return value.trim(); }
}
class Profile extends FormatHelpers {}
// A plain format function would be clearer.
Code patterns
These patterns focus on the pieces developers actually reuse: object literals, methods, constructors, prototypes, getters, copying, classes and inheritance.
Subclass inherits parent methods.
class Widget {
label() { return "Widget"; }
}
class Panel extends Widget {}
console.log(new Panel().label());
Initialize parent state first.
class Widget {
constructor(title) { this.title = title; }
}
class Panel extends Widget {
constructor(title, size) {
super(title);
this.size = size;
}
}
Subclass can customize behavior.
class Widget { label() { return "Widget"; } }
class Panel extends Widget {
label() { return "Panel"; }
}
super can call parent methods too.
class Widget { label() { return "Widget"; } }
class Panel extends Widget {
label() { return `${super.label()} Panel`; }
}
Rules that matter
Objects are where JavaScript programs start to model real application state. Good object shape makes later functions, classes and UI updates much easier to maintain.
A subclass should truly be a specialized parent.
Subclass constructors must initialize parent state first.
Deep inheritance becomes hard to debug.
Passing functions or objects is often simpler.
Changing parent behavior should be obvious.
Inherited behavior can break in subtle ways.
Production thinking
Inheritance is powerful, but it creates coupling. Use it when it clarifies the model, not just to reuse a few lines of code.
Inherited UI components should not hide accessible behavior in a distant parent class.
Production class hierarchies should stay shallow and have clear contracts between parent and subclass.
Inherited render methods must still produce predictable visible content and metadata.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Object-oriented JavaScript becomes much easier when you can explain where the data lives, where the method is found and which object receives this.
Chapter checkpoint
Finish this chapter by turning the lessons into a small practical proof.
Build a small example that combines three lessons from this chapter.
Can you explain the important tradeoff without reading from the page?