Lesson content
Read, practise, then check your understanding
Classes provide strict prototype-based constructor syntax. Methods live on the prototype; fields initialize per instance.
Encapsulated model
class Account {
#balance = 0;
constructor(owner) { this.owner = owner; }
deposit(amount) {
if (amount <= 0) throw new RangeError("positive amount required");
this.#balance += amount;
}
get balance() { return this.#balance; }
static from(data) { return new Account(data.owner); }
}
extends and super build inheritance, while # fields enforce privacy. Methods lose their receiver when passed bare, so bind or wrap callbacks. Prefer composition for reusable capabilities and inheritance only for genuine substitutability. Constructors cannot be async; use an async factory when setup requires awaiting.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.