Object-Oriented Programming (OOP) in JavaScript is a paradigm that organizes code using objects, which bundle data and behavior together. JavaScript supports OOP through prototypes and the modern class syntax.

💡 Core Concepts
- Objects: Collections of properties and methods.
- Classes: Blueprints for creating objects.
- Inheritance: Objects or classes can inherit features from others.
- Encapsulation: Hiding internal details and exposing only what is necessary.
Creating Objects
Objects can be created in multiple ways. The simplest is using object literals:
📌 Deep Dive: Object Literal
const person = {
name: 'Alice',
greet() {
return `Hello, my name is ${this.name}`;
}
};
person.greet(); // "Hello, my name is Alice"
Using Constructor Functions
Before class syntax, constructor functions were used to create multiple similar objects:
📌 Deep Dive: Constructor Function
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
return `Hi, I'm ${this.name}`;
};
const bob = new Person('Bob');
bob.greet(); // "Hi, I'm Bob"
ES6 Classes
Classes provide a cleaner syntax to create objects and handle inheritance:
📌 Deep Dive: Class Syntax
class Person {
constructor(name) {
this.name = name;
}
greet() {
return `Hello, I'm ${this.name}`;
}
}
const jane = new Person('Jane');
jane.greet(); // "Hello, I'm Jane"
Inheritance with Classes
Use extends to create a subclass and super() to call the parent class constructor:
📌 Deep Dive: Class Inheritance
class Employee extends Person {
constructor(name, position) {
super(name);
this.position = position;
}
describe() {
return `${this.name} works as a ${this.position}`;
}
}
const emp = new Employee('Mark', 'Developer');
emp.greet(); // "Hello, I'm Mark"
emp.describe(); // "Mark works as a Developer"
Mark works as a Developer
Encapsulation with Private Fields
Use the # prefix to declare private fields accessible only within the class:
📌 Deep Dive: Private Fields
class Counter {
#count = 0;
increment() {
this.#count++;
}
getCount() {
return this.#count;
}
}
const c = new Counter();
c.increment();
c.getCount(); // 1
// c.#count; // SyntaxError: Private field '#count' must be declared in an enclosing class
💡 Summary
- Objects combine data and functions as properties and methods.
- Constructor functions and ES6 classes create reusable blueprints.
- Inheritance allows new classes to extend existing ones.
- Private fields encapsulate internal state for better modularity.
⚠️ Remember
JavaScript’s prototype-based inheritance differs from classical OOP languages. Understanding prototypes helps grasp how inheritance and method lookup work under the hood.
| Constructor Functions | ES6 Classes |
|---|---|
| Function-based with prototype methods | Class syntax with methods inside class body |
| Explicit prototype assignment | Inheritance with extends and super() |
| More verbose and older style | Cleaner, modern syntax |
| Can be less intuitive for beginners | Closer to classical OOP syntax |
Quick Knowledge Check
Test what you just learned
Question 1 of 2
Which keyword is used to call the parent class constructor inside a subclass?
Question 2 of 2
How do you declare a private field in an ES6 class?
Loading results...