Academy
ProgrammingJS IntermediateClasses & Object-Oriented Programming

Classes & Object-Oriented Programming

The Problem Classes Solve

You know how to create one object. But what if you need a hundred students, each with the same shape — name, marks, subject, a method to compute grade?

Copy-pasting the object literal a hundred times is not an option. You need a blueprint — something you define once and stamp out as many times as you like.

The grade method is identical in every object but duplicated everywhere. Change the grading rule and you must find and update every copy. A class solves this.

Your First Class

  • class defines the blueprint
  • constructor runs automatically when you write new Student(...) — it sets up the object's initial state
  • Methods defined inside the class are shared by all instances — there is only one copy of grade() in memory, regardless of how many students you create
  • new creates a fresh instance and returns it

Classes Are Syntactic Sugar

This is important to understand: JavaScript classes are not a fundamentally different system. They are a cleaner way of writing what the language was already doing with functions and prototypes.

Classes didn't add new power to JavaScript — they added readable syntax over existing prototype-based mechanics. Knowing this means you are never mystified by what's actually happening underneath.

Inheritance — Extending a Class

One class can extend another, inheriting all its properties and methods and adding its own.

super(...) calls the parent class's constructor. You must call it before accessing this in a child constructor. super.introduce() calls the parent's method — letting you extend behaviour rather than replace it entirely.

Static Methods — Belonging to the Class, Not the Instance

Sometimes a method makes sense on the class itself, not on individual objects. Use static:

Static methods are utilities that conceptually belong to the class but don't need the state of any particular instance. Math.sqrt, Array.isArray, Object.keys — these are all static methods on built-in classes.

Private Fields — True Encapsulation

JavaScript now has real private fields using the # prefix. They are completely inaccessible from outside the class — not just by convention, but enforced by the language:

The get balance() syntax defines a getter — a method that looks like a property when you read it (account.balance, not account.balance()). Getters and setters let you control read/write access to private data.

A Complete Example — Putting It Together

Private #students array, public getters for derived values, method chaining via return this — this is what a well-designed class looks like in practice.

What You Have Learned

Classes are blueprints for creating many objects of the same shape — with shared methods, inheritance, and genuine encapsulation.

The key ideas:

  • class + constructor + new — define the blueprint, set up state, stamp out instances
  • Methods defined in a class are shared by all instances — one copy in memory, not one per object
  • Classes are syntactic sugar over JavaScript's prototype system — same mechanics, cleaner syntax
  • extends + super — inherit everything from a parent class, override what you need
  • static methods belong to the class itself, not to any instance
  • #privateField — truly private, enforced by the language, not just convention
  • Getters (get prop()) let you expose computed values as though they are plain properties

In the next lesson: Error Handlingtry, catch, finally, custom error classes, and how to write code that fails gracefully rather than crashing silently or catastrophically.