Cohort2026ChaiCodewebdevJavaScript

JavaScript OOPs Explained: Classes, Objects, and Blueprints ️

Hello everyone! If you've already mastered basic JavaScript Objects, it's time to level up. Today, we are diving into Object-Oriented Programming (OOP)…

Kenil Patel
Kenil PatelFull Stack Engineer
JavaScript OOPs Explained: Classes, Objects, and Blueprints ️

Hello everyone! If you've already mastered basic JavaScript Objects, it's time to level up. Today, we are diving into Object-Oriented Programming (OOP). The name sounds heavy, but believe me, it is just a smarter and much more organized way to write code.

Let's break it down using simple, real-world analogies!

1. What Does OOP Mean?

In standard programming, we often write our data and our functions separately. But in OOP, we pack our data (properties) and the functions that work on that data (methods) together into a single "box". We call this box an Object.

The main philosophy of OOP is: "Design your code like the real world." This makes managing and reusing your code (Code Reusability) incredibly easy.


2. Real-World Analogy: Blueprints and Objects

The best way to understand OOP is to imagine a Car Blueprint.

Think about a company manufacturing a new car. They don't just start building; first, they create a blueprint (a design). That blueprint states that the car will have 4 wheels, a steering wheel, and a color option.

  • Blueprint (Class): This is just a design. You cannot drive a blueprint.

  • Actual Cars (Objects): Using that single blueprint, the company can manufacture thousands of real, drivable cars (a red car, a blue car, a black car).

We do the exact same thing in JavaScript. We write the design (Class) once, and then we create as many objects as we want from it. That’s the magic of Code Reusability.


3. What is a Class in JavaScript?

In JavaScript, a Class is the template or blueprint used to create objects. We build it using the class keyword. It is a standard practice to start a class name with a Capital letter.

Let's create a simple Car class:

JavaScript

class Car {
  // Class structure goes here
}

That's it! You have an empty class. Now let's add some life to it.


4. The constructor Method ️

Whenever we create a new object from our class, we need a way to set its initial data (like its color or brand). For this, we use a special function called the constructor. This method runs automatically the moment a new object is created.

JavaScript

class Car {
  constructor(brandName, carColor) {
    this.brand = brandName;
    this.color = carColor;
  }
}

Note: The this keyword here simply means "this current object." It refers to the specific car we are building at that exact moment.


5. Creating Objects Using Classes (Instantiation)

Now our blueprint (Class) is ready. Let's build some actual cars (Objects) from it. We do this using the new keyword.

JavaScript

// Building the first car
let car1 = new Car("Tata", "Red");

// Building the second car
let car2 = new Car("Mahindra", "Black");

console.log(car1); // Output: Car { brand: 'Tata', color: 'Red' }
console.log(car2); // Output: Car { brand: 'Mahindra', color: 'Black' }

See that? We wrote the class just once, but we created multiple objects with different details. This is why OOP is so powerful for code reusability!


6. Methods Inside a Class (Actions) ‍️

Objects don't just store data; they can also perform actions. When we write a function inside a class, we call it a Method.

Let's add a method to our Car class that starts the engine:

JavaScript

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

  // This is a method
  startEngine() {
    console.log(`Vroom! The \({this.color} \){this.brand} is starting...`);
  }
}

let myCar = new Car("Tesla", "White");
myCar.startEngine(); // Output: Vroom! The White Tesla is starting...

7. The Basic Idea of Encapsulation

Encapsulation is a fundamental concept in OOP. Just like a medical capsule bundles different medicinal powders safely inside a shell, Encapsulation means bundling the Object's data (properties) and its methods into a single, safe unit (the Class).

This keeps your code highly organized, prevents it from being scattered everywhere, and keeps it safe from interference by outside code.


Would you like me to generate English text versions of the cover pages for these blogs before you submit your assignment?