Day 72 – Object Oriented Programming (OOP) in JavaScript
Today I explored one of the most powerful programming paradigms — Object Oriented Programming (OOP) in JavaScript.
OOP helps us structure code using real-world concepts like objects, classes, inheritance, and methods.
🔹 Creating a Class in JavaScript
class Sample {
constructor(name, place){
this.n = name;
this.p = place;
}
findAvg(chemistry, physics, maths){
return `${this.n} from ${this.p} got ${(chemistry + physics + maths) / 3}`;
}
}
const obj = new Sample("Nasil", "Marutha");
console.log(obj.findAvg(55, 65, 74));
✅ Key Learnings:
class keyword is used to define a blueprint.
constructor() runs automatically when an object is created.
this refers to the current object.
Methods are called using the object (obj.method()).
🔹 Inheritance in JavaScript
Inheritance allows a child class to access properties and methods of a parent class.
class Car {
constructor(brand){
this.car_name = brand;
}
present(){
return `I have a ${this.car_name}`;
}
}
class Model extends Car {
constructor(brand, model){
super(brand);
this.mod = model;
}
show(){
return `${this.present()}, the model is ${this.mod}`;
}
}
const obj = new Model("BMW", "X6");
console.log(obj.show());
✅ Key Concepts:
extends → used for inheritance
super() → calls the parent class constructor
Child class can use both parent and its own methods
Method overriding happens if child defines same method
🔹 Important Differences (JS vs Python Understanding)
JavaScript uses constructor()
Python uses __init__()
JavaScript uses extends
Python uses Child(Parent)
JavaScript uses super()
Python uses super().__init__()
💡 Key Takeaway:
OOP makes code modular, reusable, and scalable. Understanding classes, constructors, this, inheritance, and super() builds a strong foundation for advanced JavaScript development.
Step by step, moving towards mastering JavaScript.
#JavaScript #OOP #WebDevelopment #FrontendDevelopment #Programming
I'm realizing that I need to add a database but I'm weak on backend stuff. For now, it is helping me learn Python and I know it will help with C#, but a DB is a definite!