Object-Oriented Programming in JavaScript
A Story from the Mahabharata (With Code & Memory)

The morning of the great Kurukshetra war had arrived. The battlefield stretched endlessly. Thousands of warriors stood ready. Chariots creaked, bows tightened, and conch shells echoed across the plains.
Arjuna stood in his chariot, looking across the field. He saw teachers, cousins, friends, and rivals. The battlefield was enormous and filled with countless warriors.
Arjuna turned toward Krishna and said,
“Madhava… this battlefield is too vast. There are so many warriors, each with different skills, weapons, and responsibilities. How does anyone manage something this complex?”
Krishna smiled calmly.
“Arjuna, every complex system becomes manageable when it follows structure. The Kuru kingdom itself works because every warrior follows a defined role.”
Krishna then continued,
“This is the same principle wise engineers follow when they build systems.”
And thus began a conversation that strangely mirrors the philosophy of Object-Oriented Programming in JavaScript.
The Idea of a Warrior
Krishna pointed toward the army and said,
“Before any warrior was born, the kingdom already had an idea of what a warrior should be.”
Every warrior would have:
a name
a weapon
the ability to introduce themselves in court
This idea existed long before Arjuna, Bhima, or Karna.
In programming, such an idea is called a class.
class warrior{
constructor(name, weapon){
this.name=name;
this.weapon=weapon;
}
introduce(){
console.log(`I am \({this.name} and I fight with \){this.weapon}`);}}
Krishna continued,
“This is like Dronacharya writing the rulebook of warriors. It defines what every warrior must have.”
But at this moment there are still no real warriors in the code.
Only the blueprint exists.
When the Warriors Were Born
Krishna then reminded Arjuna of their childhood.
Years ago, the princes of Hastinapur trained under Dronacharya.
Arjuna mastered the bow.
Bhima mastered the mace.
Karna became one of the greatest archers.
The idea of a warrior had now turned into real individuals.
In JavaScript, we create them as objects.
const arjuna=new warrior('Arjuna', 'bow');
const bhima = new Warrior("Bhima", "Mace");
const karna = new Warrior("Karna", "Divine Bow");
Now Arjuna can speak.
arjuna.introduce();
Output
I am Arjuna and I fight with Bow
Krishna said,
“Just as warriors are born from a dynasty, objects are created from classes.”
Each warrior follows the same structure but carries their own identity.
Where Do These Warriors Live?
Arjuna listened carefully but asked other question.
“Krishna… when we create these warriors in code, where do they actually exist?”
Krishna replied,
“Inside the kingdom of memory.”
In JavaScript memory is divided into two regions.
Memory Area | Purpose |
Stack | stores references |
Heap | stores objects |
When we create Arjuna:
const Arjuna=new Warrior("Arjuna", "Bow")
Something important happens.
Inside Heap memory, JavaScript creates the warrior object.
Conceptually:
Heap
{
name: "Arjuna",
weapon: "Bow"
}
But the variable arjuna itself only stores a reference.
Stack
arjuna → memory address
Heap
{ name: "Arjuna", weapon: "Bow" }
Krishna explained,
“The warrior lives in the kingdom, while the royal record simply points to him.”
The Wisdom of Dronacharya
Bhima once asked Arjuna,
“Brother, how do you always know how to introduce yourself?”
Arjuna laughed.
“Because Dronacharya taught us.”
Krishna explained that the same principle exists in JavaScript.
The function introduce() is not copied into every object. Instead, it lives in a shared place called the prototype.
Conceptually-
Warrior.prototype
|
introduce()
When we call:
arjuna.introduce();
JavaScript performs a search.
First it checks the object itself.
arjuna
name
weapon
introduce ❌
Then it checks the prototype.
Warrior.prototype
introduce
Krishna said,
“Just as warriors share the wisdom of their teacher, objects share methods through the prototype.”
This system is called the prototype chain.
The Secret Astras
Krishna then looked toward Arjuna and said,
“Some knowledge in this world must remain protected.”
In the Mahabharata, certain weapons like Brahmastra were secret. Not everyone could access them.
Similarly, in programming we protect certain information.
class Warrior {
#secretWeapon;
constructor(name, weapon, secretWeapon) {
this.name = name;
this.weapon = weapon;
this.#secretWeapon = secretWeapon;
}
revealSecret() {
console.log(`\({this.name}'s secret weapon is \){this.#secretWeapon}`);
}
}
Now create Arjuna again.
const arjuna = new Warrior("Arjuna", "Bow", "Brahmastra");
arjuna.revealSecret();
But this will fail:
console.log(arjuna.#secretWeapon);
Krishna explained,
“Some knowledge must remain hidden. This is called encapsulation.”
Warriors with Different Skills
Krishna then pointed toward Bhima.
“Not every warrior fights the same way.”
Arjuna fights with arrows
Bhima fights with a mace
In programming we represent this with inheritance.
class Archer extends Warrior {
shootArrow() {
console.log(`${this.name} shoots an arrow`);
}
}
class MaceWarrior extends Warrior {
smash() {
console.log(`${this.name} strikes with a mace`);
}
}
Now create the warriors.
const arjuna = new Archer("Arjuna", "Bow", "Brahmastra");
const bhima = new MaceWarrior("Bhima", "Mace", "Vajra Strike");
arjuna.shootArrow();
bhima.smash();
Here both classes reuse the base Warrior structure.
The Battle Begins
As the conch shells sounded, warriors began attacking in different ways.
Arjuna fired arrows.
Bhima swung his mace.
Yet both were performing the same action — attack.
In programming this idea becomes polymorphism.
class Warrior {
attack() {
console.log("Warrior attacks");
}
}
class Archer extends Warrior {
attack() {
console.log("Shoots arrows");
}
}
class MaceWarrior extends Warrior {
attack() {
console.log("Strikes with mace");
}
}
Now watch the battle.
const arjuna = new Archer();
const bhima = new MaceWarrior();
arjuna.attack();
bhima.attack();
Output-
Shoots arrows
Strikes with mace
Same method.
Different behavior.
The Lesson Krishna Wanted Arjuna to Understand
Krishna finally turned to Arjuna and said,
“A battlefield works because every warrior has a defined role and follows the traditions of the dynasty.”
Software systems follow the same principle.
Without structure:
code becomes chaotic
systems become impossible to maintain
Object-Oriented Programming provides that structure.
Classes define blueprints.
Objects represent real entities.
Inheritance builds relationships.
Encapsulation protects knowledge.
Polymorphism enables flexibility.
“A well-designed program is not very different from a well-organized kingdom.”




