ES6 Classes
Classes provide syntactic sugar over prototype-based inheritance. They support constructors, methods, static methods, getters/setters, and extends.
Classes provide syntactic sugar over prototype-based inheritance. They support constructors, methods, static methods, getters/setters, and extends.
class Shape {
#area = 0;
constructor(color) { this.color = color; }
get info() { return `${this.color} shape`; }
static create(c) { return new Shape(c); }
}
class Circle extends Shape {
constructor(r, c) { super(c); this.#area = Math.PI * r * r; }
get area() { return this.#area; }
}
Private class fields (#field) are native to JS since ES2022 — no need for WeakMap workarounds.
More in JavaScript