Classes
MediumIn plain terms
A class is a blueprint for creating objects. You define properties and methods once, then create many "instances" that share that structure. The constructor method runs when you create a new instance with the new keyword.
Classes support inheritance with extends: a child class can reuse and extend a parent class. The super keyword calls the parent's constructor or methods. Under the hood, JavaScript still uses prototypes—classes are a cleaner syntax on top of the prototype system. They make object-oriented code easier to write and understand.
What you need to know
- •class, constructor
- •extends, super
- •Syntactic sugar over prototypes
Try it yourself
Copy the code below and run it in your browser console or a code editor:
class Person {
constructor(name) {
this.name = name;
}
greet() {
return 'Hi, ' + this.name;
}
}
class Student extends Person {
constructor(name, grade) {
super(name);
this.grade = grade;
}
}Why this matters
Classes are the standard way to do OOP in modern JS. Interviewers ask how classes relate to prototypes, when to use super(), and how inheritance works under the hood.
How it connects
Classes are syntactic sugar over prototypes (prototypes topic). this in methods refers to the instance; super calls the parent. Connects to OOP concepts: encapsulation (private fields), inheritance (extends), polymorphism (override).
Interview focus
Be ready to explain these; they come up often in JS interviews.
- ▸Classes are syntactic sugar over constructor + prototype. typeof class is "function".
- ▸super() must be called in child constructor before using this. super.method() for parent methods.
- ▸Private fields (#) for encapsulation; static for class-level methods.
Learn more
Dive deeper with these trusted resources: