In JavaScript, every object has a hidden property called [[Prototype]]. This is a reference to another object.
This “lookup” process continues until the property is found or the chain reaches null.
myArray $\rightarrow$ Array.prototype $\rightarrow$ Object.prototype $\rightarrow$ null.This is why you can use methods like .push() on an array even though you didn’t define .push() yourself—it’s inherited from the Array.prototype.
Before ES6 classes, this was the primary way to create multiple objects of the same “type.”
JavaScript
function Robot(name) {
this.name = name;
}
// Adding a method to the Prototype (Memory Efficient)
Robot.prototype.greet = function() {
console.log(`Beep Boop, I am ${this.name}`);
};
const r1 = new Robot("R2D2");
const r2 = new Robot("C3PO");
r1.greet(); // Works
r2.greet(); // Works
Performance Tip: We put methods on the
prototypeso that every instance shares one copy of the function in memory, rather than creating a new function for every single robot.
Object.create()This method creates a new object and allows you to specify exactly which object should be its prototype.
JavaScript
const animal = {
eats: true,
walk() { console.log("Animal walking..."); }
};
const dog = Object.create(animal);
dog.bark = function() { console.log("Woof!"); };
dog.walk(); // "Animal walking..." (Inherited)
console.log(dog.eats); // true (Inherited)
If you add a property to an object that has the same name as a property in its prototype, the object’s property “shadows” (overwrites) the prototype’s property for that specific instance.
JavaScript
const vehicle = { wheels: 4 };
const motorbike = Object.create(vehicle);
motorbike.wheels = 2; // Shadowing
console.log(motorbike.wheels); // 2
console.log(vehicle.wheels); // 4 (Original remains unchanged)
You can inspect the prototype relationship using these modern methods:
Object.getPrototypeOf(obj): Returns the prototype of obj.obj.hasOwnProperty('prop'): Checks if a property belongs to the object itself or if it’s inherited.JavaScript
console.log(dog.hasOwnProperty('bark')); // true
console.log(dog.hasOwnProperty('eats')); // false (Inherited)
| Feature | Prototypal (JS) | Classical (Java/C++) |
| Foundation | Objects inherit from Objects. | Classes inherit from Classes. |
| Flexibility | Highly dynamic; can change prototypes at runtime. | Rigid; structure defined at compile time. |
| Memory | Instances share methods via the chain. | Instances get copies or use v-tables. |
Warning: Do not do this in production (it’s called “Monkey Patching”), but it’s great for understanding how JS works.
JavaScript
// Adding a custom method to the global Array prototype
Array.prototype.lastItem = function() {
return this[this.length - 1];
};
const fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits.lastItem()); // "Cherry"
Objects are the building blocks of modern applications.
In JavaScript, objects are not copied from classes — they inherit directly from other objects using prototypes.
Understanding this module helps you master:
An object is a collection of key–value pairs, where:
Example (conceptual):
user = {
name: "Prakash",
age: 24,
login()
}
this to access object dataconst user = { name: "A", age: 20 }
new Object()const user = new Object()
function User(name) {
this.name = name
}
Object.create()Creates object with a specified prototype.
const user = Object.create(proto)
class User {
constructor(name) {}
}
⚠️ Internally still uses prototypes.
user.nameuser["name"]Use bracket when:
Each property has:
valuewritableenumerableconfigurableUsed to control:
Objects are mutable:
Important for:
Prototypal inheritance means:
Objects can inherit properties and methods from another object via a prototype.
No classes required.
Every JavaScript object has:
[[Prototype]]__proto__ (internal)When property not found:
nullprototype vs __proto__| Term | Meaning |
|---|---|
prototype | Property of constructor function |
__proto__ | Link from object to prototype |
This distinction is interview-critical.
Example flow:
new User() → User.prototype → Object.prototype → null
Methods defined on prototype are shared across instances.
Child object can:
If child defines same method:
Objects delegate behavior to prototype.
Combines constructor + prototype methods.
Adds extra features during inheritance.
| Feature | Classes | Prototypes |
|---|---|---|
| Syntax | Clean | Verbose |
| Under the hood | Prototypes | Prototypes |
| Inheritance | extends | Prototype chain |
Always prefer prototype methods for efficiency.