Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

JS
JavaScript
0/12 completed
Beginner
What is JavaScript?Variables and data typesOperators and expressionsMaking decisions: if and switchLoops
Intermediate
FunctionsArrays and array methodsObjects
Advanced
Scope and closuresClasses and OOPPromises and async/await
Pro
The event loop
Tracks / JavaScript / Advanced

Classes and OOP

Object-oriented programming (OOP) organises code around objects that bundle data (properties) and behaviour (methods). A class is a blueprint for creating many similar objects.

Defining a class

JavaScript · editable
  • constructor runs automatically when you call new Product(...).
  • this refers to the specific object being created or used.
  • Methods are shared by all instances, so they aren't copied into each one.

Inheritance with extends

A class can extend another and reuse its code. Call super(...) to run the parent's constructor.

JavaScript · editable

Private fields, getters and setters

Fields that start with # are truly private: code outside the class can't read them.

JavaScript · editable

Composition over inheritance

Deep inheritance trees become hard to change. Pros often prefer composition: building objects out of smaller pieces.

JavaScript · editable

The four OOP pillars, briefly

Pillar Meaning In JS
Encapsulation Hide internal details #private fields, closures
Abstraction Expose a simple interface Public methods like deposit()
Inheritance Reuse a parent's behaviour extends, super
Polymorphism Same method, different behaviour Overriding speak()

Practice

Create a Shape class with an area() method that returns 0, and subclasses Circle(radius) and Rectangle(w, h) that override it. Then print the total area of an array of shapes.

JavaScript · editable
✎ Check your understanding

1.What must a subclass constructor call before using this?

2.How do you make a truly private field in a JavaScript class?

← Scope and closures