Understanding Object-Oriented Programming in JavaScript
What Object-Oriented Programming (OOP) Means
Object-Oriented Programming (OOP) is a programming approach where we organize code using objects and classes.
Instead of writing separate code again and again, we create reusable structures that represent real-world entities.
In OOP, objects contain data (properties) and behavior (methods).
This makes programs:
Easier to understand
Easier to maintain
More reusable
JavaScript supports OOP concepts using classes and objects.
Real-World Analogy (Blueprint → Objects)
To understand OOP better, imagine a car blueprint.
A blueprint defines how a car should be built:
color
model
speed
Using this blueprint, we can create many cars.
Similarly in programming:
Class → Blueprint
Object → Real instance created from blueprint
Example:
Car Blueprint → Car1, Car2, Car3
All cars are created from the same blueprint but they can have different values.
What is a Class in JavaScript
A class is like a template or blueprint used to create objects.
It defines:
properties (data)
methods (functions)
In JavaScript, classes were introduced in ES6 to make OOP easier to write and understand.
Basic syntax:
class Person {
}
This class does not yet contain properties or methods, but it acts as a structure for creating objects.
Creating Objects Using Classes
Once a class is created, we can create objects from it using the new keyword.
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const person1 = new Person("Rahul", 20);
const person2 = new Person("Aman", 22);
Here:
Personis the classperson1andperson2are objects created from the class
Each object has its own data.
Constructor Method
A constructor is a special method that runs automatically when a new object is created.
It is mainly used to initialize object properties.
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
Here:
nameandageare parametersthis.nameandthis.agestore the values inside the object
So whenever we create a new object, the constructor assigns the values.
Methods Inside a Class
Methods are functions defined inside a class that describe the behavior of objects.
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log("Hello, my name is " + this.name);
}
}
const person1 = new Person("Rahul", 20);
person1.greet();
Output:
Hello, my name is Rahul
Methods allow objects to perform actions.
Basic Idea of Encapsulation
Encapsulation means keeping data and methods together inside a class and controlling access to them.
This helps:
protect data
organize code better
Example:
class BankAccount {
constructor(balance) {
this.balance = balance;
}
deposit(amount) {
this.balance += amount;
}
}
Here the balance and deposit behavior are wrapped inside the same class.
Assignment Example: Student Class
Let’s create a Student class as an example.
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
printDetails() {
console.log("Name: " + this.name + ", Age: " + this.age);
}
}
const student1 = new Student("Rohan", 20);
const student2 = new Student("Anjali", 21);
student1.printDetails();
student2.printDetails();
Output:
Name: Rohan, Age: 20
Name: Anjali, Age: 21
Here we created multiple student objects from the same class.
Why OOP is Useful
Object-Oriented Programming helps in:
Code reusability
Better organization of code
Easier maintenance
Modeling real-world entities in programs
Because of these advantages, OOP is widely used in modern software development.