Object-Oriented Programming (OOP) in JavaScript

Software developer writing to think clearly about code, systems, and real-world tradeoffs.
As programs grow, managing data and behavior becomes harder.
Object-Oriented Programming (OOP) helps organize code by grouping data and related functions together.
In JavaScript, OOP is often implemented using classes and objects.
What Object-Oriented Programming Means
Object-Oriented Programming is a way of writing code where:
Data is stored in objects
Behavior is defined through methods
Objects are created from classes
In simple terms:
OOP models software based on real-world entities.
Examples:
| Real World | Programming |
|---|---|
| Car | Car object |
| Student | Student object |
| Bank account | Account object |
Each object contains:
Properties → data
Methods → actions
Real-World Analogy: Blueprint → Objects
Imagine a car blueprint used in a factory.
The blueprint describes:
color
model
engine
start() method
From one blueprint, many cars can be produced.
Programming works the same way.
Blueprint (Class)
↓
Creates
↓
Objects (Instances)
Example:
Car Class → Car1, Car2, Car3
Each car object has the same structure but different values.
What Is a Class in JavaScript
A class is a template for creating objects.
It defines:
properties
methods
Basic syntax:
class Person {
}
A class itself does nothing until objects are created from it.
Creating Objects Using Classes
Objects are created using the new keyword.
Example:
class Person {}
const person1 = new Person();
Here:
Person→ classperson1→ object
Constructor Method
The constructor runs automatically when an object is created.
It is used to initialize properties.
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
Now create objects:
const person1 = new Person("Rahul", 22);
const person2 = new Person("Amit", 25);
Result:
person1 → {name: "Rahul", age: 22}
person2 → {name: "Amit", age: 25}
Methods Inside a Class
Methods define actions objects can perform.
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log("Hello my name is " + this.name);
}
}
Create object:
const person1 = new Person("Rahul", 22);
person1.greet();
Output:
Hello my name is Rahul
Here:
greet()is a methodthis.namerefers to the object's property
Example: Car Class
class Car {
constructor(brand, model) {
this.brand = brand;
this.model = model;
}
start() {
console.log(this.brand + " car started");
}
}
Create objects:
const car1 = new Car("Toyota", "Camry");
const car2 = new Car("Honda", "Civic");
car1.start();
car2.start();
Output
Toyota car started
Honda car started
Basic Idea of Encapsulation
Encapsulation means keeping data and behavior together in one unit (class).
Example:
Student
├── name
├── age
└── printDetails()
Instead of writing scattered code, everything is organized in one place.
Benefits:
cleaner code
easier maintenance
better structure
Encapsulation is a core concept in Object-Oriented Programming.
Blueprint → Object Diagram
CLASS (Blueprint)
Car
┌───────────────────┐
│ brand │
│ model │
│ start() │
└───────────────────┘
│
Creates objects
│
┌────────┴────────┐
│ │
Car1 Car2
Toyota Camry Honda Civic
Class → Instance Relationship
Class
Person
│
├── person1 → Rahul
├── person2 → Amit
└── person3 → Neha
One class can generate many objects.
Why OOP Is Useful
OOP helps with:
code reusability
better organization
easier debugging
scalable applications
Modern frameworks built with JavaScript—like Node.js and React—often use OOP concepts internally.
Assignment
1. Create a Student Class
Create a class:
class Student {
}
Add properties:
name
age
2. Add a Method
Create a method:
printDetails()
Example output:
Name: Rahul
Age: 21
3. Create Multiple Objects
Example:
const student1 = new Student("Rahul", 21);
const student2 = new Student("Amit", 22);
4. Call the Method
student1.printDetails()
student2.printDetails()
Key Takeaway
Object-Oriented Programming organizes code using:
Classes → blueprint
Objects → real instances
Properties → data
Methods → actions
Understanding these basics is essential before moving into deeper topics like:
inheritance
polymorphism
abstraction



