2.7 Object-Oriented JavaScript – Mastering Objects and Classes (EASY Mode)

Table of Contents

Hey Web Codders! πŸ‘‹ It’s me, Vikas Sankhla, your coding buddy. Today we’re diving into something super cool:
Object-Oriented JavaScript (OOP).

Ever wondered how big apps like games, shopping carts, or social media are built? πŸ€”
Yup, they use OOP to organize code nicely so it’s easier to build, manage, and grow.

Let’s learn it together in the simplest way possible! 🧠✨


🧱 What is Object-Oriented Programming (OOP)?

Okay, imagine LEGO blocks. 🧱

Each block (or piece) has its own shape, color, and job.
When you connect them, you build something BIGGER and BETTER. πŸš€

In JavaScript:

  • Each block = Object
  • The full build = App

OOP is a way to break code into objects that hold data + actions.


πŸ’‘ Why Use OOP?

  • βœ… Keeps your code organized
  • βœ… Helps reuse code (no need to rewrite!)
  • βœ… Makes big apps easy to manage

πŸ› οΈ How to Create an Object (The Basics)

Let’s say we want to describe a car. πŸš—

We can make an object like this:

let car = {
brand: "Tesla",
model: "Model 3",
color: "Red",
start: function() {
console.log("Vroom! The car has started. πŸ”₯");
}
};

console.log(car.brand); // Tesla
car.start(); // Vroom! The car has started. πŸ”₯

What’s happening here?

  • brand, model, color βž” Properties
  • start() βž” Method (an action)

Diagram Showing A Car Object With Labeled Properties And Methods
2.7 Object-Oriented Javascript – Mastering Objects And Classes (Easy Mode) 5


🏫 Classes – The Blueprint πŸ“

Think of a class like a recipe. 🍳
You write the recipe once, and you can bake many cakes! πŸŽ‚

In JavaScript, a class is a blueprint to create objects.

Here’s how we write a class:

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

    start() {
        console.log(`${this.brand} is starting! πŸš—`);
    }
}

We use the constructor() to set up the car’s details.


πŸ”¨ Create an Object from a Class

jsCopyEditlet myCar = new Car("Tesla", "Model 3", "Red");
myCar.start();  // Tesla is starting! πŸš—

1707292754375 Image8
2.7 Object-Oriented Javascript – Mastering Objects And Classes (Easy Mode) 6


πŸ”‘ What is this?

When we use this, it means:
πŸ‘‰ β€œI’m talking about this object right here.”

Example:

jsCopyEditconsole.log(this.brand);  // Points to the car’s brand

So inside the class, this.brand means the brand of the car we are working with.


πŸ‘¨β€πŸ‘©β€πŸ‘¦ Inheritance – Kids Get Stuff from Parents

Inheritance means:
One class (the child) gets features from another class (the parent). πŸ‘¨β€πŸ‘©β€πŸ‘¦

Example:

jsCopyEditclass ElectricCar extends Car {
    charge() {
        console.log(`${this.brand} is charging! ⚑`);
    }
}

Now let’s use it:

jsCopyEditlet myEV = new ElectricCar("Tesla", "Model X", "Black");
myEV.start();    // Tesla is starting! πŸš—
myEV.charge();   // Tesla is charging! ⚑

Wow! πŸš€ ElectricCar can:

βœ… Use things from Car
βœ… Add new stuff like charge()


Flowchart For Ev Charging Algorithm 2
2.7 Object-Oriented Javascript – Mastering Objects And Classes (Easy Mode) 7


πŸ”„ Prototypes – The Secret Sauce

Every object in JavaScript has a prototype. πŸ§ͺ
It’s like a hidden backpack that holds extra powers.

Example:

jsCopyEditCar.prototype.honk = function() {
    console.log(`${this.brand} goes beep beep! πŸ“£`);
};

myCar.honk();  // Tesla goes beep beep! πŸ“£

We added a new method to ALL cars after creating them!


πŸŒ€ Polymorphism – Same Action, Different Behavior

Poly-what? πŸ˜…
Let’s keep it simple:

Polymorphism = Different objects do the same thing in their own way.

Example:

jsCopyEditclass Bike {
    start() {
        console.log("The bike zooms off! 🏍️");
    }
}

let myBike = new Bike();
myCar.start();   // Tesla is starting! πŸš—
myBike.start();  // The bike zooms off! 🏍️

Same method name: start()
Different results. πŸ’₯


πŸ”’ Encapsulation – Keep Things Private

Encapsulation means hiding details and only showing what’s needed.

Example:

jsCopyEditclass BankAccount {
    #balance = 0;  // Private property

    deposit(amount) {
        this.#balance += amount;
        console.log(`Balance is now $${this.#balance}`);
    }
}

let myAccount = new BankAccount();
myAccount.deposit(100);  // Balance is now $100
console.log(myAccount.#balance);  // ❌ ERROR: Private field!

βœ… You can deposit money
❌ But you can’t directly see or change #balance

SAFE! πŸ”


Https Dev To Uploads.s3.Amazonaws.com Uploads Articles Jnjgfl10Cn4Tm9Qlztv1
2.7 Object-Oriented Javascript – Mastering Objects And Classes (Easy Mode) 8


πŸ”„ Quick Recap Table

ConceptMeaningExample
ObjectA thing with data + actionslet car = { brand: "Tesla", start: ... }
ClassA blueprint for making objectsclass Car { ... }
ConstructorSets up new objectsconstructor(brand, model) { ... }
InheritanceOne class gets stuff from anotherclass ElectricCar extends Car
PrototypeHidden backpack of methodsCar.prototype.honk = ...
PolymorphismSame method, different behaviorstart() in Car & Bike
EncapsulationHiding private details#balance in BankAccount

🎯 Conclusion

And there you have it, Web Codders! πŸŽ‰ You just mastered OOP in JavaScript:

  • βœ”οΈ Classes & Objects
  • βœ”οΈ Constructors & this
  • βœ”οΈ Inheritance & Prototypes
  • βœ”οΈ Polymorphism & Encapsulation

OOP helps you build cleaner, smarter, and stronger apps. πŸ’ͺ

πŸ‘‰ Want to level up faster?
Subscribe now: YouTube – Web Codder
πŸ“Έ Follow on Insta: @web_codder_official
πŸ’¬ Join the chat: WhatsApp group

Let’s keep coding and having fun! πŸš€

Share the Post:
Picture of Web Codder

Web Codder

Vikas Sankhla is a seasoned Full Stack Developer with over 7 years of experience in web development. He is the founder of Web Codder, a platform dedicated to providing comprehensive web development tutorials and resources. Vikas specializes in the MERN stack (MongoDB, Express.js, React.js, Node.js) and has been instrumental in mentoring aspiring developers through his online courses and content. His commitment to simplifying complex web technologies has made him a respected figure in the developer community.

Related Posts