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