Understanding Object-Oriented Programming in JavaScript
When applications become large, it becomes difficult to manage code if everything is written in simple functions. To organize code better, JavaScript provides a programming style called Object-Oriented Programming (OOP).
OOP helps developers create structured, reusable, and easy-to-maintain code.
1. What Object-Oriented Programming (OOP) Means
Object-Oriented Programming (OOP) is a programming approach where we organize code using objects and classes.
An object contains:
Properties → data
Methods → actions or functions
Example:
A car object might have:
properties → color, brand, speed
methods → start(), stop(), accelerate()
OOP helps us group data and behavior together.
2. Real-World Analogy: Blueprint → Objects
A simple way to understand OOP is by using a blueprint example.
Think about building cars.
A blueprint describes how a car should look and behave.
Many cars can be created from the same blueprint.
In programming:
The blueprint = class
The car created from blueprint = object
Example:
Car blueprint:
brand
color
speed
From this blueprint we can create many cars.
Example:
Car 1 → Toyota, Red
Car 2 → Honda, Black
Car 3 → BMW, White
All of them follow the same structure.
3. What Is a Class in JavaScript
A class is like a blueprint used to create objects.
JavaScript introduced the class keyword in ES6.
Example:
class Person {
}
This class currently has no properties or methods, but it defines a structure.
4. Creating Objects Using Classes
Objects are created from classes using the new keyword.
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const person1 = new Person("Harshal", 25);
const person2 = new Person("Amit", 30);
Here:
person1andperson2are objectsBoth are created from the Person class
5. Constructor Method
The constructor is a special method inside a class.
It runs automatically when an object is created.
It is used to initialize properties.
Example:
class Car {
constructor(brand, color) {
this.brand = brand;
this.color = color;
}
}
const car1 = new Car("Toyota", "Red");
console.log(car1.brand);
console.log(car1.color);
Output:
Toyota
Red
The constructor helps assign values when creating objects.
6. Methods Inside a Class
Classes can also contain methods, which are functions inside the class.
Example:
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log("Hello, my name is " + this.name);
}
}
const user = new Person("Harshal");
user.greet();
Output:
Hello, my name is Harshal
Here:
greet()is a methodIt belongs to the Person class
7. Basic Idea of Encapsulation
Encapsulation means keeping related data and functions together inside one object or class.
Example:
class Student {
constructor(name, marks) {
this.name = name;
this.marks = marks;
}
showMarks() {
console.log(this.name + " scored " + this.marks);
}
}
const student1 = new Student("Amit", 85);
student1.showMarks();
Here:
nameandmarksare datashowMarks()is the method
Both are kept inside the Student class.
This makes the program organized and easier to understand.