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