Understanding Object-Oriented Programming in JavaScript
Modern applications in JavaScript often deal with complex data and behaviors.
To organize code better, developers use a programming style called Object-Oriented Programming (OOP).
OOP helps us structure programs using objects and classes, making code easier to manage, reuse, and understand.
What is Object-Oriented Programming (OOP)?
Object-Oriented Programming is a programming approach where we organize code into objects that contain data and functions.
In simple words:
Objects store information
Methods perform actions on that information
Example:
A student object might have:
name
age
course
And a method like:
- displayStudentDetails()
This approach helps keep related data and behavior together.
Real-World Analogy: Blueprint → Objects
Think about building cars.
A blueprint describes how a car should look and behave:
color
engine
speed
start()
From that blueprint, we can create many cars.
In programming:
| Real World | Programming |
|---|---|
| Blueprint | Class |
| Actual Car | Object |
| Features | Properties |
| Actions | Methods |
So a class acts as a blueprint, and objects are created from that blueprint.
What is a Class in JavaScript?
A class is a template used to create objects.
Classes were introduced in modern JavaScript through ES6.
Example class:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
Here:
Personis the classnameandageare properties
Creating Objects Using Classes
Objects are created using the new keyword.
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
let person1 = new Person("Rahul", 25);
let person2 = new Person("Amit", 30);
console.log(person1.name);
Output:
Rahul
Each object has its own data.
Constructor Method
The constructor is a special method inside a class.
It runs automatically when an object is created.
Example:
class Car {
constructor(brand, year) {
this.brand = brand;
this.year = year;
}
}
When we create an object:
let myCar = new Car("Toyota", 2022);
The constructor automatically assigns:
brand = Toyota
year = 2022
Methods Inside a Class
Classes can also contain methods (functions inside the class).
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log("Hello, my name is " + this.name);
}
}
Using the method:
let user = new Person("Ravi", 22);
user.greet();
Output:
Hello, my name is Ravi
Basic Idea of Encapsulation
Encapsulation means keeping related data and methods together inside a class.
Example:
class BankAccount {
constructor(balance) {
this.balance = balance;
}
deposit(amount) {
this.balance += amount;
}
}
Here:
balanceis stored inside the objectdeposit()modifies it
So data and behavior stay together.
This makes code organized and easier to maintain.
Assignment Practice
Create a Student Class
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
printDetails() {
console.log("Name: " + this.name);
console.log("Age: " + this.age);
}
}
Create Multiple Student Objects
let student1 = new Student("Rahul", 20);
let student2 = new Student("Priya", 21);
student1.printDetails();
student2.printDetails();
Output:
Name: Rahul
Age: 20
Name: Priya
Age: 21
Each student object has its own properties but shares the same class structure.
Visual Representation
Class → Objects
Student (Class)
│
├── student1 → Rahul, 20
└── student2 → Priya, 21
The class defines the structure, while objects store actual values.
Why OOP is Important
Object-Oriented Programming helps developers:
Organize large programs
Reuse code easily
Model real-world systems
Keep code clean and maintainable
You will see OOP concepts used heavily in frameworks like React, backend development, and large-scale applications.