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
โ Propertiesstart()
โ Method (an action)

๐ซ 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! ๐

๐ 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()

๐ 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! ๐

๐ Quick Recap Table
Concept | Meaning | Example |
---|---|---|
Object | A thing with data + actions | let car = { brand: "Tesla", start: ... } |
Class | A blueprint for making objects | class Car { ... } |
Constructor | Sets up new objects | constructor(brand, model) { ... } |
Inheritance | One class gets stuff from another | class ElectricCar extends Car |
Prototype | Hidden backpack of methods | Car.prototype.honk = ... |
Polymorphism | Same method, different behavior | start() in Car & Bike |
Encapsulation | Hiding 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! ๐