SyntaxStudy
Sign Up
JavaScript Intermediate 5 min read

Classes

ES6 Classes

Classes provide syntactic sugar over prototype-based inheritance. They support constructors, methods, static methods, getters/setters, and extends.

Example
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; }
}
Pro Tip

Private class fields (#field) are native to JS since ES2022 — no need for WeakMap workarounds.