ποΈ Understanding Object-Oriented Programming (OOP) in JavaScript

Like to study,fitness freak,my looks is my first priority, hardworking person, like discipline and love to learn new thing
When projects grow bigger, writing everything as separate variables and functions becomes messy.
Thatβs where Object-Oriented Programming (OOP) helps.
OOP is a programming style where we organize code using objects and classes.
π What is Object-Oriented Programming (OOP)?
Object-Oriented Programming is a way of writing code using:
Classes (blueprints)
Objects (real instances)
Properties (data)
Methods (functions inside objects)
It helps in:
Organizing code
Reusing code
Making programs scalable
π Real-World Analogy: Blueprint β Objects
Think about a car factory.
A company creates a blueprint for a car.
From that blueprint, they manufacture:
Car 1
Car 2
Car 3
Each car has:
Brand
Color
Speed
The blueprint is like a class.
Each manufactured car is an object.
π Blueprint β Object Diagram
Class (Blueprint)
β
----------------
| Car Class |
----------------
β β β
Car1 Car2 Car3
(Object) (Object) (Object)
One blueprint β Multiple objects.
ποΈ What is a Class in JavaScript?
A class is a template used to create objects.
In JavaScript, we use the class keyword.
πΉ Example: Creating a Class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log("Hello, my name is " + this.name);
}
}
Letβs break it down.
π§± Constructor Method
The constructor is a special method that runs automatically when we create a new object.
It is used to initialize properties.
constructor(name, age) {
this.name = name;
this.age = age;
}
this refers to the current object.
π§ Creating Objects from a Class
Now we create objects using the new keyword.
let person1 = new Person("Ilyas", 21);
let person2 = new Person("Rahul", 22);
Now we have two different objects created from the same class.
π§ Calling Methods
person1.greet();
person2.greet();
Output:
Hello, my name is Ilyas
Hello, my name is Rahul
Each object has its own data but shares the same structure.
π Class β Instance Relationship
Class: Person
β
person1 β { name: "Ilyas", age: 21 }
person2 β { name: "Rahul", age: 22 }
Class defines structure.
Objects store actual data.
π§ Methods Inside a Class
Methods are functions written inside a class.
Example:
class Car {
constructor(brand, color) {
this.brand = brand;
this.color = color;
}
drive() {
console.log(this.brand + " is driving.");
}
}
Here:
brandandcolorβ propertiesdrive()β method
π Basic Idea of Encapsulation
Encapsulation means:
π Keeping data and related methods together inside a class.
Instead of writing:
let name = "Ilyas";
let age = 21;
function greet() {
console.log("Hello " + name);
}
We organize it like this:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log("Hello " + this.name);
}
}
Everything related to a person stays inside the Person class.
Thatβs cleaner and more organized.
π― Why OOP is Powerful
Code becomes reusable
Easy to manage large projects
Clear structure
Real-world modeling becomes easier
Instead of rewriting logic, we reuse the class.
π― Mini Assignment
1οΈβ£ Create a Student class
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
printDetails() {
console.log("Name: " + this.name + ", Age: " + this.age);
}
}
2οΈβ£ Create multiple student objects
let student1 = new Student("Aman", 20);
let student2 = new Student("Priya", 21);
student1.printDetails();
student2.printDetails();
You just created reusable student objects.
π‘ Final Thoughts
Object-Oriented Programming helps you:
Structure code properly
Avoid repetition
Build scalable applications
Model real-world systems
Almost all modern frameworks (React, Angular, Node.js apps) rely heavily on OOP concepts.
Master this, and your JavaScript level jumps significantly.
