OOP & Classes
Object-oriented programming in JavaScript — class syntax, constructors, inheritance, and how it all maps to prototypes under the hood.
What it is, in plain English: A class is a blueprint for creating objects. It defines the properties an object will have and the methods (functions) it can perform. JavaScript's class syntax (introduced in ES2015) gives you a clean way to write object-oriented code, but it's built on JavaScript's prototype system — understanding both levels makes you a stronger developer.
Classes — blueprints for creating objects
So far you've created objects one at a time with curly braces: { name: "Alice", age: 30 }.
That works fine for one object. But what if you need 100 users, each with the same structure
and the same behaviours? You'd repeat yourself constantly. A class solves this:
write the blueprint once, create as many objects as you need.
Defining a class
class User {
// constructor runs automatically when you do: new User(...)
constructor(name, email) {
this.name = name; // 'this' refers to the new object being created
this.email = email;
this.createdAt = new Date();
}
// Methods — functions available on every User object
greet() {
return `Hello, I'm ${this.name}`;
}
isAdmin() {
return this.email.endsWith("@admin.com");
}
}
// Create instances with 'new'
const alice = new User("Alice", "alice@example.com");
const bob = new User("Bob", "bob@admin.com");
console.log(alice.greet()); // "Hello, I'm Alice"
console.log(bob.greet()); // "Hello, I'm Bob"
console.log(alice.isAdmin()); // false
console.log(bob.isAdmin()); // true
console.log(alice.name); // "Alice" — access properties directly
console.log(alice.createdAt); // the creation dateInheritance — one class extends another
Inheritance lets you create a specialised version of a class that has everything the parent has, plus more.
class Shape {
constructor(colour) {
this.colour = colour;
}
describe() {
return `A ${this.colour} shape`;
}
}
class Circle extends Shape {
constructor(colour, radius) {
super(colour); // MUST call super() first — sets up the parent
this.radius = radius;
}
area() {
return Math.PI * this.radius ** 2;
}
// Override the parent's describe()
describe() {
return `A ${this.colour} circle with radius ${this.radius}`;
}
}
class Rectangle extends Shape {
constructor(colour, width, height) {
super(colour);
this.width = width;
this.height = height;
}
area() {
return this.width * this.height;
}
}
const c = new Circle("red", 5);
const r = new Rectangle("blue", 4, 6);
console.log(c.describe()); // "A red circle with radius 5"
console.log(c.area().toFixed(2)); // "78.54"
console.log(r.describe()); // "A blue shape" — inherited, not overridden
console.log(r.area()); // 24Static methods — belong to the class, not instances
class MathHelper {
static square(n) { return n * n; }
static cube(n) { return n * n * n; }
static clamp(n, min, max) { return Math.min(Math.max(n, min), max); }
}
// Call on the class itself, NOT on an instance:
console.log(MathHelper.square(5)); // 25
console.log(MathHelper.clamp(15, 0, 10)); // 10
// This would fail:
// const m = new MathHelper();
// m.square(5); // TypeError: m.square is not a functionChecking type: instanceof
class Animal {}
class Dog extends Animal {}
const rex = new Dog();
console.log(rex instanceof Dog); // true
console.log(rex instanceof Animal); // true — Dog extends Animal
console.log(rex instanceof Object); // true — everything is an Object
Try It Yourself
Official Sources
- MDN Web Docs — Classes
- MDN Web Docs — Inheritance and the prototype chain
- ECMAScript 2024 — Class Definitions (§15.7)
- MDN Web Docs — Private class features
The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.