JavaScript OOP: From Blueprints to Reality

Published: (March 15, 2026 at 02:12 PM EDT)
4 min read
Source: Dev.to

Source: Dev.to

Cover image for JavaScript OOP: From Blueprints to Reality

Introduction

Up until now, you’ve likely been writing code procedurally—writing a line, then another, then another. It’s like keeping a recipe on a loose scrap of paper. But what happens when you want to open a global chain of restaurants? You can’t rely on scraps of paper. You need a system.

In JavaScript, Object‑Oriented Programming (OOP) is that system. It allows us to model our code after the real world.

1. The Big Idea: Blueprint vs. Reality

The best way to understand OOP is the Car Factory analogy.

  • The Blueprint (The Class): Defines that every car must have 4 wheels, a color, and an engine. It also says cars can “Drive” and “Brake.”
  • The Actual Car (The Object/Instance): The physical car that rolls off the assembly line. One might be a Red Tesla, another a Blue Ford. Both follow the same blueprint but are individual objects.

2. What is a Class in JavaScript?

A class is a reserved keyword in JavaScript (introduced in ES6) that acts as the template for creating objects. It groups data (properties) and behavior (methods) together.

Before classes, code could become messy:

let student1Name = "Veer";
let student1Age = 20;

let student2Name = "Ansh";
let student2Age = 22;
// This gets messy fast!

3. The “Constructor”: The Setup Phase

Every class can define a special method called constructor. Think of it as the assembly line that runs when a new object is created.

Creating the Student Blueprint

class Student {
    // The constructor is the 'Setup' function
    constructor(name, age, course) {
        this.name = name;   // 'this' refers to the specific student being built
        this.age = age;
        this.course = course;
    }

    // This is a method (action)
    introduce() {
        console.log(`Hi, I'm ${this.name}, a ${this.age}-year-old studying ${this.course}.`);
    }
}

Why use this?
Inside a class, this points to the particular instance being created, allowing each object to store its own data.

4. Creating Objects: The new Keyword

To create an actual object from a blueprint, use the new keyword. This process is called instantiation.

// Manufacturing two unique students from one blueprint
const student1 = new Student("Veer", 20, "Web Dev");
const student2 = new Student("Ansh", 22, "Data Science");

// They both have the same "actions"
student1.introduce(); // Hi, I'm Veer...
student2.introduce(); // Hi, I'm Ansh...

5. The Power of Encapsulation

Encapsulation—one of OOP’s four pillars—means hiding complex logic inside a class. The rest of the program only needs to know what a method does, not how it does it.

  • More Organized: All “Student”‑related code lives in one place.
  • Easier to Debug: If introduce() breaks, you have a single location to inspect.
  • Reusable: Create thousands of students with just a few lines of code.

6. Procedural vs. Object‑Oriented Style

FeatureProcedural (Old Way)OOP (The Modern Way)
LogicFunctions and data are separate.Data and logic are bundled together.
ScalingHard to manage as the app grows.Designed for large, complex apps.
AnalogyA list of instructions.A collection of interacting “things.”

7. Summary Challenge: Build Your Library

Try the following in your console:

  1. Create a class called Book.
  2. Add a constructor that takes title, author, and year.
  3. Add a method called getAge that returns Current Year - year.
  4. Create two objects (your favorite books).
  5. Log the result of .getAge() for both.
class Book {
    constructor(title, author, year) {
        this.title = title;
        this.author = author;
        this.year = year;
    }

    getAge() {
        const currentYear = new Date().getFullYear();
        return currentYear - this.year;
    }
}

// Example usage
const book1 = new Book("1984", "George Orwell", 1949);
const book2 = new Book("The Pragmatic Programmer", "Andrew Hunt & David Thomas", 1999);

console.log(`${book1.title} is ${book1.getAge()} years old.`);
console.log(`${book2.title} is ${book2.getAge()} years old.`);

Final Thought

OOP is more than just syntax; it’s a mindset. It encourages you to think of your code as a world of interacting objects rather than a linear list of tasks. Master it, and you can build anything—from a simple todo app to a massive social media platform.

0 views
Back to Blog

Related posts

Read more »

Intro About Java Script

Introduction In today’s class I learned a short introduction to JavaScript, so I’ll share some facts about JavaScript in this blog. What Is JavaScript? JavaScr...