Object-Oriented Programming (OOP)

Last Updated: 24th October 2025


  • JavaScript is a prototype-based object-oriented language — meaning, it allows you to create and manage objects and reuse code through prototypes and classes.
  • Prototypes are a way to add new properties and methods to existing objects, without modifying the object itself.
  • Classes are a way to create a blueprint for creating objects with common properties and methods.

Hinglish Tip 🗣: OOP ko samjho jaise ek real-world model — “Object” ek entity hoti hai (jaise Car, Student, User) jiske properties (data) aur methods (actions) hote hain.


Why OOP?

  • Reusability → Code can be reused easily.
  • Organization → Code is structured and easy to understand.
  • Scalability → Easy to add new features.
  • Real-world mapping → Works like real objects (Car, Student, BankAccount).

Six Pillars of OOP

  • Class → Blueprint
  • Object → Real entity
  • Polymorphism → Different behavior
  • Abstraction → Hide complex logic
  • Inheritance → Reuse code
  • Encapsulation → Data hiding

Example

class Car {
  constructor(brand) {
    this.brand = brand;
  }

  start() {
    console.log(`${this.brand} is starting...`);
  }
}

class ElectricCar extends Car {
  charge() {
    console.log(`${this.brand} is charging ⚡`);
  }
}

const tesla = new ElectricCar("Tesla");
tesla.start(); // Tesla is starting...
tesla.charge(); // Tesla is charging ⚡

Hinglish Tip 🗣: Class = Design, Object = Reality. Jaise “Car” ek design hai aur “Tesla” us design ka ek example.