H.dev

Objects in OOP

Objects as Instances

In Object-Oriented Programming (OOP), an Object is an instance of a Class. If the class is the blueprint for a car, the object is the actual physical car you drive out of the dealership.

While we previously learned about generic object literals ({}), OOP focuses on objects that are systematically created from a class structure.

Creating an Object (Instantiation)

To create a new object from a class, we use the new keyword. This process is called instantiation.

class Developer {
    code() {
        console.log("Writing some JavaScript...");
    }
}

// Instantiating a new object
const devHimanshu = new Developer();
const devJohn = new Developer();

devHimanshu.code(); // Outputs: "Writing some JavaScript..."

In the example above, devHimanshu and devJohn are two completely separate, independent objects created from the exact same Developer blueprint.

Objects vs Object Literals

Why go through the trouble of creating a class when you can just make an object literal?

Object Literal:

const himanshu = {
    name: "Himanshu",
    code() {
        console.log("Coding...");
    }
}

If you only need exactly one of something (like a single configuration object for your app), an object literal is perfect. But if you are making a video game and need to spawn 100 enemies on the screen, writing 100 object literals by hand is impossible. That is where OOP objects shine!

💡 Himanshu's Tip:

When you console.log an object created from a class, you will notice that the class name is prefixed in the console output (e.g., Developer {}). This helps you quickly identify the blueprint that was used to create the object during debugging.

Interview Questions

  1. What does the new keyword do in JavaScript?
  2. What is the difference between an Object Literal and an instantiated Object?
  3. If two objects are instantiated from the same class, do they share the same memory location?

Design & Developed by Himanshu Dubey © 2026