Understanding Object-Oriented Programming in Javascript

Object-oriented programming is a concept of writing code in which we organize everything around objects.
An object is a combination of
Properties
Methods
Instead of writing separate variables and functions, we bundle them together.
Real-World Analogy: Blueprint β Objects
Letβs understand object-oriented programming with a simple example:
Imagine you are a car manufacturer.
You first create a blueprint (design)
- Then you use that blueprint to create multiple cars
π In JavaScript:
Blueprint = Class
Car = Object (Instance)
So instead of rewriting code again and again, we define a class once and reuse it.
π§± What is a Class in JavaScript?
A class is a blueprint used to create objects.
It defines:
What properties an object will have
What actions it can perform
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hi, my name is ${this.name}`);
}
}
π§ͺ Creating Objects (Instances)
We use the new keyword to create objects from a class.
const person1 = new Person("Ashish", 25);
const person2 = new Person("Rahul", 30);
person1.greet(); // Hi, my name is Ashish
person2.greet(); // Hi, my name is Rahul
π Each object:
Has its own data
Shares the same structure and methods
βοΈ Understanding the Constructor
The constructor is a special method inside a class.
Key Points:
It runs automatically when an object is created
It initializes object properties
constructor(name, age) {
this.name = name;
this.age = age;
}
π this refers to the current object.
π§ Methods Inside a Class
Methods define what an object can do.
greet() {
console.log(`Hi, my name is ${this.name}`);
}
π Methods are shared across all instances (memory efficient).
π Basic Idea of Encapsulation
Encapsulation means:
π Keeping data and methods together and controlling access
Real-world example:
Think of a car:
You donβt directly control the engine
You use steering, brakes, accelerator
Same in code:
Internal data is hidden
Access is controlled via methods
Simple Example:
class BankAccount {
constructor(balance) {
this.balance = balance;
}
deposit(amount) {
this.balance += amount;
}
getBalance() {
return this.balance;
}
}
π We interact using methods instead of directly modifying data.
β»οΈ Why Use OOP?
OOP helps you write better code:
β Reusable (write once, use many times)
β Organized (structured code)
β Scalable (easy to expand)
β Maintainable (easy to debug and update)
π§ Class vs Object (Quick Comparison)
| Concept | Meaning |
|---|---|
| Class | Blueprint |
| Object | Real instance created from class |
π― Assignment
Try this yourself π
Task:
Create a Student class with:
Properties:
name,ageMethod:
printDetails()
Example Solution:
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
printDetails() {
console.log(`Name: \({this.name}, Age: \){this.age}`);
}
}
const student1 = new Student("Aman", 20);
const student2 = new Student("Neha", 22);
student1.printDetails();
student2.printDetails();
π§© Visual Understanding (Conceptual)
Class (Blueprint)
β
Create using new
β
Objects (Instances)
Example:
Car (Class)
β
car1, car2, car3 (Objects)
π Common Mistakes Beginners Make
β Forgetting new keyword β Not using this properly β Thinking each object has separate methods (they are shared) β Confusing class with object
π§ Final Thoughts
OOP is not just a conceptβitβs a way of thinking.
Once you understand:
Class
Object
Constructor
Methods
You can build real-world applications much more easily π
Happy Coding! π»



