Understanding Object-Oriented Programming in JavaScript

What is Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is a programming approach where we organize our code using classes and objects instead of only variables and functions.
Using OOP helps in making the code more reusable, cleaner, and easier to manage. It also keeps the program well-structured.
Real-world example (Blueprint → Objects)
Think about how cars are manufactured.
Before producing cars, engineers first create a blueprint or design. Once the design is ready, many cars can be built from that same design.
OOP works in a similar way.
First we create a class, which works like a blueprint. After that we can create multiple objects from that class, each having its own values.
What is a Class in JavaScript
A class can be understood as a blueprint that defines what kind of data and behavior an object will have.
Inside a class, we usually have two main parts:
Constructor – used to initialize or create the data for the object.
Methods – functions that perform operations using that data.
Example:
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
printDetails() {
console.log(`Name: \({this.name}, Age: \){this.age}`);
}
}
Creating Objects from a Class
To create objects from a class, we use the new keyword.
It creates a new instance of the class and assigns values to it.
Example:
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
printDetails() {
console.log(`Name: \({this.name}, Age: \){this.age}`);
}
}
let s1 = new Student("Divyansh", 19);
let s2 = new Student("Pooja", 20);
console.log(s1); // Student { name: 'Divyansh', age: 19 }
console.log(s2.name); // Pooja
Here, s1 and s2 are objects created from the same class but they contain different data.
Constructor Method
The constructor() method defines what kind of data the object will store.
It is automatically called when a new object is created from the class.
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log("Hi, I am " + this.name);
}
}
In this example, the constructor creates two properties: name and age.
Methods Inside a Class
Methods are simply functions written inside a class.
They perform actions using the data stored in the object.
Example:
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
printDetails() {
console.log(`Name: \({this.name}, Age: \){this.age}`);
}
}
let s1 = new Student("Divyansh", 19);
let s2 = new Student("Pooja", 20);
s1.printDetails(); // Name: Divyansh, Age: 19
s2.printDetails(); // Name: Pooja, Age: 20
Each object can call the same method but it will use its own data.
Basic Idea of Encapsulation
Encapsulation means keeping related data and functions together inside a class so that the data is protected and organized.
Example:
class BankAccount {
constructor(balance) {
this.balance = balance;
}
deposit(amount) {
this.balance += amount;
}
}
Here, balance and the deposit() method both belong to the BankAccount class.
They are grouped together so that the account data is handled only through the class methods.
Encapsulation helps make the program more secure, organized, and easier to maintain.



