PROGRAMMING-CONCEPTS

Object: Definition, Purpose, and Examples

An object is a specific instance created from a class. It represents a single, self-contained entity that combines state (data stored in variables or properties) and behavior (actions performed through methods). Objects are where programs actually come to life—while classes define structure, objects are the working pieces that hold real data and perform actions during execution.

Objects form the foundation of object-oriented programming (OOP). They let developers build systems that mirror real-world concepts, like users, cars, or invoices, by grouping together the data that describes them and the operations that affect them.


Understanding What an Object Is

When you define a class, you’re setting up a pattern. When you create an object, you’re building a real, usable version of that pattern. Each object stores its own data but follows the same rules defined by its class.

In Python:

class Car:
    def __init__(self, brand, color):
        self.brand = brand
        self.color = color

    def drive(self):
        print(f"The {self.color} {self.brand} is driving.")

car1 = Car("Toyota", "blue")
car2 = Car("Tesla", "red")

car1.drive()
car2.drive()

Here, car1 and car2 are two different objects with their own data. They both come from the same blueprint, but each holds unique values.

In JavaScript and TypeScript, the idea is the same:

class Car {
  constructor(public brand: string, public color: string) {}
  drive() {
    console.log(`The ${this.color} ${this.brand} is driving.`);
  }
}

const car1 = new Car("Toyota", "blue");
const car2 = new Car("Tesla", "red");
car1.drive();

Objects are the “living” form of a class definition—they exist in memory, respond to commands, and carry individual data sets.


Object State and Behavior

Every object has state and behavior.

  • State refers to its current data—the values stored in its properties.
  • Behavior refers to the actions it can perform through its methods.

For example, in a simple banking program, an account object might have a balance (state) and methods to deposit or withdraw money (behavior).

class Account:
    def __init__(self, owner, balance):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

When you create two Account objects, each one keeps its own balance. Calling deposit() on one doesn’t affect the other.

This separation of state and behavior gives programs their modularity. Instead of working with scattered variables, you interact with self-contained entities that know how to manage their own data.


Objects in JavaScript Without Classes

JavaScript is a bit different from languages like Python and Swift—it allows you to create objects directly without a class. These are known as object literals.

const user = {
  name: "Luna",
  age: 25,
  greet() {
    console.log(`Hi, I'm ${this.name}.`);
  }
};

user.greet();

Object literals are a lightweight way to group related data and behavior. They’re used heavily in frameworks like React, where components behave like objects that encapsulate both data (state) and actions (methods).

You can also create dynamic objects in JavaScript using factory functions—functions that return new objects without a class definition.

function createUser(name) {
  return {
    name,
    greet() {
      console.log(`Hello, ${name}!`);
    }
  };
}

const user = createUser("Nova");
user.greet();

This flexibility makes JavaScript a “prototype-based” language, where even without formal class structures, objects remain at the heart of everything.


Object Relationships

Real-world software rarely deals with isolated objects. They interact, depend on each other, and sometimes contain other objects. These relationships are key to building scalable systems.

Association describes when one object references another. For example, a Customer object might contain a reference to a ShoppingCart.

class ShoppingCart:
    def __init__(self):
        self.items = []

class Customer:
    def __init__(self, name):
        self.name = name
        self.cart = ShoppingCart()

The Customer and ShoppingCart are distinct but connected—changing one doesn’t automatically destroy the other.

Aggregation is a looser relationship, where an object uses another without owning it entirely. A Team might contain players who can exist independently.

Composition, on the other hand, means one object fully owns another. If the parent is destroyed, the contained object disappears too. A Car composed of Engine and Tire objects is a classic example—remove the car, and those parts no longer exist as functional units.

These relationship patterns make code reflect real-world dependencies, helping developers design programs that behave predictably as systems grow more complex.


Mutable vs. Immutable Objects

Objects are typically mutable, meaning their internal data can change over time. For example:

car1.color = "green"

However, immutability—where objects cannot be modified once created—has become increasingly popular in modern programming, especially in functional programming and UI frameworks.

In React, for instance, immutability ensures predictable state updates. Instead of modifying an object directly, you create a new one:

const updatedUser = { ...user, age: 26 };

Swift enforces immutability through constants (let) and optional mutability for class properties, giving developers fine-grained control over when and how state changes.


Object Patterns and Reuse

Objects often serve as reusable models that simplify large systems. Instead of passing multiple variables between functions, you can pass a single object that carries all necessary data.

Objects also form the basis for design patterns such as Singletons, which ensure only one instance exists across the program, or Observers, where multiple objects react to changes in another’s state. These patterns rely on how objects communicate and share data with each other.

In web development, objects underpin almost everything—from DOM elements represented as JavaScript objects to APIs sending JSON objects as structured data. When you fetch user data from a server:

const response = await fetch("/api/user");
const user = await response.json();
console.log(user.name);

you’re transforming text-based data into an object your program can directly manipulate.


Common Mistakes with Objects

A common beginner mistake is mixing up classes and objects—using the terms interchangeably or expecting one to behave like the other. The class defines how something works; the object is what actually exists in memory.

In JavaScript, developers often run into issues with this, where the reference to the current object is lost if a method is called out of context. Using arrow functions or binding methods can fix this.

Another mistake is overloading objects with too many responsibilities—so-called “god objects.” Each object should represent one clear concept. Splitting large, multipurpose objects into smaller ones improves maintainability and clarity.


Summary

An object is a working instance that combines data and behavior into one cohesive unit. It’s what makes a class useful—where structure turns into functionality. Objects maintain their own state, perform their own actions, and interact with other objects to create complete systems.

While the Class defines the plan, the Object is the result. It’s the difference between describing what a car looks like and actually driving one. Understanding how objects store data, interact, and depend on one another is key to mastering not just object-oriented programming, but programming as a whole.

Learn to Code for Free
Start learning now
button icon
To advance beyond this tutorial and learn to code by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

Reach your coding goals faster