DEV Community

Cover image for Object-Oriented Programming in JavaScript: The Blueprint Factory You Never Knew You Needed
Janmejai Singh
Janmejai Singh

Posted on

Object-Oriented Programming in JavaScript: The Blueprint Factory You Never Knew You Needed

"Good code is like a well-organized factory โ€” everything has its place, and every worker knows their job."

Modern software development isn't just about writing code โ€” it's about organizing code so it's reusable, scalable, and maintainable. That's exactly what Object-Oriented Programming (OOP) gives you.

If you're learning JavaScript, understanding OOP is a game-changer. Let's break it down the right way โ€” with real-world analogies, clean diagrams, and code you can actually use.


๐Ÿ“Œ Table of Contents

  1. What is OOP?
  2. The Blueprint Analogy
  3. Classes in JavaScript
  4. The Constructor Method
  5. Adding Methods to Classes
  6. Encapsulation โ€” Keeping Things Tidy
  7. Hands-On Example: Student Class
  8. Why OOP Actually Matters

What is OOP? {#what-is-oop}

Object-Oriented Programming (OOP) is a paradigm where you design software using objects โ€” self-contained units that bundle together related data and behavior.

Instead of scattering variables and functions across your codebase, OOP groups everything logically.

Real-World Entity Programming Object
๐Ÿš— Car Car object with brand, color, start()
๐ŸŽ“ Student Student object with name, age, enroll()
๐Ÿฆ Bank Account Account object with balance, deposit(), withdraw()
๐Ÿ“ฑ Phone Phone object with model, battery, call()

Every object has:

  • Properties โ€” what it is (data/state)
  • Methods โ€” what it does (functions/behavior)

The Blueprint Analogy {#the-blueprint-analogy}

Here's the mental model that makes OOP click for most developers:

Imagine you're running a car factory.

Before manufacturing a single car, your engineers design a blueprint. That blueprint defines the shape, engine type, wheel count, and color options.

But here's the key insight โ€” the blueprint is not a car. It's just the template.

From that one blueprint, your factory then produces:

๐Ÿ“„ Blueprint (Class)
        โ”‚
        โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚                                      โ”‚
        โ–ผ                                      โ–ผ
๐Ÿš— Car #1: Red Tesla    ๐Ÿš™ Car #2: Blue BMW    ๐Ÿš• Car #3: Black Audi
Enter fullscreen mode Exit fullscreen mode

In JavaScript terms:

Factory Concept OOP Concept
Blueprint Class
Car produced Object (Instance)
Manufacturing process new keyword

This is the entire foundation of OOP โ€” one blueprint, unlimited objects.


Classes in JavaScript {#classes-in-javascript}

A class is JavaScript's way of defining a blueprint.

class ClassName {
  constructor() {
    // Initialize properties here
  }

  methodName() {
    // Define behavior here
  }
}
Enter fullscreen mode Exit fullscreen mode

Let's create our Car class:

class Car {
  constructor(brand, color) {
    this.brand = brand;
    this.color = color;
  }
}
Enter fullscreen mode Exit fullscreen mode

Now let's manufacture some cars using the new keyword:

let car1 = new Car("Tesla", "Red");
let car2 = new Car("BMW", "Blue");

console.log(car1); // Car { brand: 'Tesla', color: 'Red' }
console.log(car2); // Car { brand: 'BMW', color: 'Blue' }
Enter fullscreen mode Exit fullscreen mode

Two completely different objects, one shared blueprint. โœ…


The Constructor Method {#the-constructor-method}

The constructor is a special method that runs automatically the moment you create a new object.

Think of it as the assembly line worker who sets up each car as it rolls off the production line.

class Person {
  constructor(name, age) {
    this.name = name; // Assigns 'name' to this specific object
    this.age = age;   // Assigns 'age' to this specific object
  }
}

let person1 = new Person("Rahul", 22);
let person2 = new Person("Anita", 24);

console.log(person1.name); // "Rahul"
console.log(person2.age);  // 24
Enter fullscreen mode Exit fullscreen mode

The this keyword refers to the current object being created. It's how each object gets its own unique property values.

๐Ÿ’ก Quick Rule: Every class should have a constructor. It's where you set up the initial state of your object.


Adding Methods to Classes {#adding-methods-to-classes}

Properties define what an object is. Methods define what an object does.

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

  // Method โ€” defines behavior
  start() {
    console.log(`${this.brand} car has started. ๐Ÿš€`);
  }

  stop() {
    console.log(`${this.brand} car has stopped.`);
  }

  describe() {
    console.log(`This is a ${this.color} ${this.brand}.`);
  }
}
Enter fullscreen mode Exit fullscreen mode

Using the methods:

let car1 = new Car("Tesla", "Red");

car1.start();    // Tesla car has started. ๐Ÿš€
car1.describe(); // This is a Red Tesla.
car1.stop();     // Tesla car has stopped.
Enter fullscreen mode Exit fullscreen mode

Each object calls its own methods and accesses its own properties via this. No conflicts, no collisions.


Encapsulation โ€” Keeping Things Tidy {#encapsulation}

Encapsulation is one of OOP's core principles. It simply means: keep related data and behavior bundled together inside a class.

Instead of having a balance variable floating around globally and separate deposit/withdraw functions scattered across files, you organize everything in one place:

class BankAccount {
  constructor(owner, initialBalance) {
    this.owner = owner;
    this.balance = initialBalance;
  }

  deposit(amount) {
    this.balance += amount;
    console.log(`โœ… Deposited โ‚น${amount}. New balance: โ‚น${this.balance}`);
  }

  withdraw(amount) {
    if (amount > this.balance) {
      console.log("โŒ Insufficient funds.");
      return;
    }
    this.balance -= amount;
    console.log(`๐Ÿ’ธ Withdrew โ‚น${amount}. New balance: โ‚น${this.balance}`);
  }

  getBalance() {
    console.log(`๐Ÿ’ฐ ${this.owner}'s balance: โ‚น${this.balance}`);
  }
}
Enter fullscreen mode Exit fullscreen mode
let myAccount = new BankAccount("Rahul", 5000);

myAccount.deposit(2000);   // โœ… Deposited โ‚น2000. New balance: โ‚น7000
myAccount.withdraw(1500);  // ๐Ÿ’ธ Withdrew โ‚น1500. New balance: โ‚น5500
myAccount.getBalance();    // ๐Ÿ’ฐ Rahul's balance: โ‚น5500
Enter fullscreen mode Exit fullscreen mode

Benefits of Encapsulation:

  • โœ… Organization โ€” related code lives together
  • โœ… Security โ€” data is protected from unintended modification
  • โœ… Reusability โ€” create as many accounts as you need from one class
  • โœ… Debuggability โ€” issues are isolated inside the class

Hands-On Example: Student Class {#hands-on-example}

Let's put everything together with a practical, real-world example.

Step 1 โ€” Define the Class

class Student {
  constructor(name, age, course) {
    this.name = name;
    this.age = age;
    this.course = course;
  }

  printDetails() {
    console.log(`
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    ๐Ÿ‘ค Name   : ${this.name}
    ๐ŸŽ‚ Age    : ${this.age}
    ๐Ÿ“š Course : ${this.course}
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    `);
  }

  greet() {
    console.log(`Hello! I'm ${this.name} and I'm studying ${this.course}.`);
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 2 โ€” Create Student Objects

let student1 = new Student("Aman",  21, "Computer Science");
let student2 = new Student("Riya",  22, "Data Science");
let student3 = new Student("Karan", 20, "Web Development");
Enter fullscreen mode Exit fullscreen mode

Step 3 โ€” Use the Objects

student1.printDetails();
student2.greet();
student3.printDetails();
Enter fullscreen mode Exit fullscreen mode

Output:

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
๐Ÿ‘ค Name   : Aman
๐ŸŽ‚ Age    : 21
๐Ÿ“š Course : Computer Science
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

Hello! I'm Riya and I'm studying Data Science.

โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
๐Ÿ‘ค Name   : Karan
๐ŸŽ‚ Age    : 20
๐Ÿ“š Course : Web Development
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
Enter fullscreen mode Exit fullscreen mode

One class. Three students. Zero repeated code. That's the power of OOP.

          CLASS: Student
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    name
    age
    course
    printDetails()
    greet()
          โ”‚
    โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    โ”‚           โ”‚           โ”‚
    โ–ผ           โ–ผ           โ–ผ
 student1    student2    student3
 Aman        Riya        Karan
 21          22          20
 CS          DS          Web Dev
Enter fullscreen mode Exit fullscreen mode

Why OOP Actually Matters {#why-oop-matters}

You might be thinking: "I could do all this with regular functions." And you're right โ€” for small scripts.

But here's what OOP gives you at scale:

Challenge Without OOP With OOP
Managing 50 students 50 separate variable sets One Student class
Adding a new feature Update code in 50 places Update the class once
Debugging Hunt across the entire codebase Isolate the specific class
Code sharing across teams Copy-paste chaos Import and reuse the class

OOP is used everywhere in serious development:

  • ๐ŸŒ Web Development โ€” React components, Node.js services
  • ๐ŸŽฎ Game Development โ€” Game entities, physics engines
  • ๐Ÿ“ฑ Mobile Apps โ€” User models, API clients
  • ๐Ÿข Enterprise Software โ€” Domain models, business logic layers

๐Ÿ”‘ Key Takeaways

Class      โ†’  The blueprint / template
Object     โ†’  An instance created from the class
Constructor โ†’  Sets up initial state when an object is created
Method     โ†’  A function that defines what an object can do
this       โ†’  Refers to the current object
new        โ†’  The keyword that creates a new object from a class
Encapsulation โ†’ Bundling data + behavior together inside a class
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ Your Challenge

Try building this on your own:

Create a Book class with:

  • Properties: title, author, pages
  • Method describe() that prints all details
  • Method isLong() that returns true if pages > 300

Create at least 3 book objects and call both methods on each.

Drop your solution in the comments below โ€” I'd love to see what you build! ๐Ÿ‘‡


What's Next?

This is just the beginning of OOP in JavaScript. In the next articles, we'll cover:

  • ๐Ÿ”— Inheritance โ€” How classes can extend other classes
  • ๐Ÿ”’ Private fields โ€” True data hiding in modern JavaScript
  • ๐Ÿงฉ Polymorphism โ€” One interface, many behaviors
  • ๐Ÿญ Abstract patterns โ€” Real-world design patterns

If this helped you, drop a โค๏ธ and follow for more JavaScript fundamentals explained clearly. No fluff, just code that makes sense.

Top comments (0)