← Learning Hub
Intermediate

Classes and Modules

A class is a blueprint for making objects with the same shape and methods. constructor runs when you make a new object with new.

Classes are useful when you need many objects of the same kind with the same behaviour - products, users, shapes in a game. The class bundles the data and the methods that work on it, so the code for "a product" lives in one place. JavaScript classes work like those in Java and Python, which makes the idea easy to carry between languages.

Example
class Product {
  constructor(name, price) {
    this.name = name;
    this.price = price;
  }
  withGst() {
    return Math.round(this.price * 1.18);
  }
  toString() {
    return `${this.name} (₹${this.price})`;
  }
}

const p = new Product("Lamp", 600);
console.log(p.withGst());     // Output: 708
console.log(String(p));       // Output: Lamp (₹600)

Inheritance with extends

Example
class SaleProduct extends Product {
  constructor(name, price, off) {
    super(name, price);        // run the parent constructor first
    this.off = off;
  }
  withGst() {
    const after = this.price - this.price * this.off / 100;
    return Math.round(after * 1.18);
  }
}

const s = new SaleProduct("Lamp", 600, 50);
console.log(s.withGst());           // Output: 354
console.log(s instanceof Product);  // Output: true

Private fields and static methods

Example
class Wallet {
  #balance = 0;                       // private: only the class can touch it
  add(amount) { this.#balance += amount; return this; }
  get balance() { return this.#balance; }
  static rupees(n) { return "₹" + n.toLocaleString("en-IN"); }
}

const w = new Wallet().add(500).add(250);
console.log(Wallet.rupees(w.balance));   // Output: ₹750

Modules: split code into files

A module is a file that exports things. Other files import them. In the browser, load the main file with <script type="module">.

As apps grow, one giant file becomes impossible to manage. Modules let you split code by job - cart logic, price calculations, UI - and say clearly what each file shares (export) and needs (import). Tools like Vite and Angular's CLI then bundle these files efficiently for the browser.

Example
// price.js
export const GST = 0.18;
export function withGst(p) { return Math.round(p * (1 + GST)); }
export default function rupees(n) { return "₹" + n; }

// main.js
import rupees, { withGst, GST } from "./price.js";
console.log(rupees(withGst(100)));   // ₹118
Modules keep code tidy: one file per job (prices, cart, user interface). Every modern framework, React and Angular included, is built from modules.
Key points
  • class, constructor and new create objects with shared methods.
  • extends and super for inheritance; # for private fields.
  • Modules: export from one file, import in another.

📝 Test yourself

1. Which keyword makes a new object from a class?
2. What must a child constructor call before using this?
3. Fill in the keyword that shares a function from a module file:
function withGst(p) { ... }