Skip to content
Objects In DepthChapter 29 / 47Intermediate4 min read

Classes

Learn the modern JavaScript class syntax, including constructor, extends, super, private

On this page

1. What are classes?

In a class, you describe what data (fields) and what actions (methods) the object will have. Then the new keyword creates objects (instances) from that blueprint.

JavaScript classes are really a cleaner syntax on top of prototypes. Methods are still stored on the prototype.

2. Why does it matter?

Writing constructor functions and prototype by hand is long and confusing. Class syntax does the same job in a shorter, more readable way.

Old constructor functionModern class
Write the function and prototype separatelyEverything in one class block
Object.create and call for inheritanceJust extends and super
Closure tricks for private dataReal private #fields
Forgetting new is a silent bugCalling a class without new throws an error

3. Where is it used?

PlaceExample
Custom errorsclass ValidationError extends Error
Data modelsObjects like User, Product, Order
ServicesAn API client class that manages a token and base URL
Web Componentsclass MyCard extends HTMLElement
GamesEntities like Player, Enemy

4. How does it work?

What happens during new User()
A new empty object is created
Its prototype is set to User.prototype
Fields are initialized
The constructor runs and this is the new object
The object is returned
PartJob
constructor()Runs once when the object is created
Public fieldcount = 0, each instance has its own value
Private field#balance, accessible only inside the class
MethodStored on the prototype, all instances share it
staticOn the class itself, not on instances
static { } blockSetup code that runs once when the class loads

5. Code example

Basic class with a private field

js
class BankAccount {
  #balance = 0;
  static count = 0;

  static {
    BankAccount.bankName = "ZnBank";
  }

  constructor(owner) {
    this.owner = owner;
    BankAccount.count++;
  }

  deposit(amount) {
    if (amount <= 0) throw new RangeError("Amount must be positive");
    this.#balance += amount;
    return this.#balance;
  }

  get balance() {
    return this.#balance;
  }
}

const acc = new BankAccount("Ali");
acc.deposit(500);
console.log(acc.balance, BankAccount.count, BankAccount.bankName);

Inheritance with extends and super

js
class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    return `${this.name} makes a sound`;
  }
}

class Dog extends Animal {
  speak() {
    return `${super.speak()}: Woof woof`;
  }
}

console.log(new Dog("Moti").speak());

How does the code work?

LineWhat is happening
#balance = 0A private field, writing acc.#balance from outside is a SyntaxError
static countA class-level counter, accessed with BankAccount.count
static { }Runs once as soon as the class is defined
get balance()A getter, read acc.balance like a property
extends AnimalDog's prototype chain is linked to Animal
super.speak()Calls the parent class's method

Interview tip

If you write a constructor in a child class, you must call super() before using this, otherwise you get a ReferenceError.

6. Common mistakes

Mistake: Using this before super() in a child constructor

Why? The parent has not initialized the object yet, so a ReferenceError is thrown.

Better: Call super(args) on the first line of the constructor, then use this.

Mistake: Passing a method as a callback and losing this

Why? In button.addEventListener('click', obj.save), this is no longer obj.

Better: Wrap it in an arrow function, or make the method a class field arrow function.

Mistake: Treating an underscore property as private

Why? _balance is only a naming convention, and anyone can change it from outside.

Better: Use a #balance private field for real privacy.

Mistake: Making a class for everything

Why? For small jobs, a class brings extra boilerplate and complexity.

Better: Plain objects and functions are enough for simple data. Use a class when state and behavior go together.

7. What to learn before and after

StageTopics
BeforeObjects, The this Keyword, Prototypes & Inheritance
AfterError Handling (custom error classes), Modules
Suggested order
Objects
Prototypes
Classes
Custom Errors and Modules

Final quick revision

PointDetail
ClassA blueprint for objects, syntax on top of prototypes
constructorRuns when an instance is created
#fieldReal private data
staticA member of the class itself, not of instances
extends / superInheritance and calling the parent
Getter / setterProperty-like access with logic inside