ES6 classes provide a cleaner syntax for creating objects with shared structure and behavior. Under the hood they use JavaScript's prototype system.
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
area() {
return this.width * this.height;
}
perimeter() {
return 2 * (this.width + this.height);
}
toString() {
return `Rectangle(${this.width}×${this.height})`;
}
}
const rect = new Rectangle(5, 3);
console.log(rect.area()); // 15
console.log(rect.perimeter()); // 16
console.log(rect.toString()); // "Rectangle(5×3)"
class Circle {
#radius; // private field (ES2022)
constructor(radius) {
this.#radius = radius;
}
get radius() { return this.#radius; }
set radius(r) {
if (r < 0) throw new Error("Radius cannot be negative");
this.#radius = r;
}
get area() { return Math.PI * this.#radius ** 2; }
}
Belong to the class itself, not instances:
class MathUtils {
static clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
}
MathUtils.clamp(15, 0, 10); // 10
BankAccount with:
(owner, initialBalance = 0) — stores owner and balance.deposit(amount) — adds amount to balance (ignore negative amounts).withdraw(amount) — subtracts amount; if insufficient funds, do nothing and return false; otherwise return true.getBalance() — returns current balance.toString() — returns "BankAccount(owner: Alice, balance: $150)"."Alice" with balance 100, deposit 50, withdraw 30, then display its toString() in #output.Class syntax: class Name { constructor(...) { this.prop = val; } }