Prototypes (Deep Dive)
⚡ JavaScript's Prototypal Inheritance
JavaScript uses prototypes for inheritance, unlike classical inheritance in languages like Java or C++. Every object has a prototype, and properties/methods can be inherited through the prototype chain.
Prototype Chain Visualization
┌─────────────────────────────────────────────────┐
│ Your Object │
│ { name: 'John', age: 30 } │
│ __proto__ → │
└─────────────────────┬─────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│ Object.prototype │
│ toString(), valueOf(), hasOwnProperty(), ... │
│ __proto__ → null │
└─────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────┐
│ Function Constructor │
│ function Person(name) { │
│ this.name = name; │
│ } │
│ __proto__ → │
└─────────────────────┬─────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│ Function.prototype │
│ apply(), call(), bind(), ... │
│ __proto__ → Object.prototype │
└─────────────────────┬─────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│ Object.prototype │
│ __proto__ → null │
└─────────────────────────────────────────────────┘Example 1: Understanding Prototypes
JavaScript Editor
🔍 Prototype Terminology
Key Concepts
- [[Prototype]]: Internal property (accessed via __proto__)
- .prototype: Property on functions
- __proto__: Getter/setter for prototype (deprecated)
- Object.getPrototypeOf(): Modern way to get prototype
- Object.setPrototypeOf(): Modern way to set prototype
Prototype Chain Rules
- Properties are looked up through the chain
- Own properties shadow inherited ones
- null is the end of every prototype chain
- Functions have Function.prototype in their chain
- Arrays have Array.prototype in their chain
Example 2: The Prototype Chain in Action
JavaScript Editor
✅ Best Practices
- Use
Object.getPrototypeOf()instead of__proto__ - Define methods on prototype for memory efficiency
- Use
hasOwnProperty()to check own properties - Consider
Object.create()for pure prototypal inheritance - Be careful with
for...in(includes inherited properties)
❌ Common Mistakes
- Modifying built-in prototypes (pollution)
- Forgetting to set constructor property
- Creating deep prototype chains (performance)
- Using
__proto__in production code - Not understanding property shadowing
Example 3: Constructor Functions and Prototypes
JavaScript Editor
📊 Constructor vs Prototype Methods
| Aspect | Constructor Methods | Prototype Methods |
|---|---|---|
| Memory Usage | ❌ Each instance gets own copy | ✅ Shared across instances |
| Performance | ❌ Slower instantiation | ✅ Faster instantiation |
| Access to Private Data | ✅ Yes (closure) | ❌ No |
| Dynamic Updates | ❌ Only affects new instances | ✅ Affects all instances |
| Use Case | Instance-specific logic | Shared behavior |
Example 4: Advanced Prototype Operations
JavaScript Editor
🎯 Prototypal Inheritance Patterns
1. Classical Pattern
function Parent() {}
function Child() {
Parent.call(this);
}
Child.prototype = Object.create(
Parent.prototype
);
Child.prototype.constructor = Child;2. Pure Prototypal
const parent = {
method() { return 'hi'; }
};
const child = Object.create(parent);
child.otherMethod = function() {
return 'hello';
};3. Factory Pattern
function createPerson(name) {
const person = Object.create(
personMethods
);
person.name = name;
return person;
}
const personMethods = {
greet() { return 'Hi'; }
};Example 5: Prototype Performance and Memory
JavaScript Editor
Exercise: Implement Prototypal Inheritance
JavaScript Editor
💡 Modern JavaScript and Prototypes
ES6 Classes (Syntactic Sugar)
// Class syntax (still uses prototypes)
class Vehicle {
constructor(make, model) {
this.make = make;
this.model = model;
}
start() {
return 'Vehicle started';
}
}
class Car extends Vehicle {
constructor(make, model, doors) {
super(make, model);
this.doors = doors;
}
honk() {
return 'Beep beep!';
}
}
// Under the hood: Still prototypes!
console.log(typeof Vehicle); // function
console.log(Vehicle.prototype.start); // functionWhen to Use Prototypes Directly
- Library/framework development
- Performance-critical code
- Working with legacy code
- Understanding JavaScript internals
- Creating dynamic inheritance structures
🎯 Interview Questions on Prototypes
- What's the difference between __proto__ and prototype?
- How does property lookup work in prototype chain?
- What happens when you modify Array.prototype?
- How would you implement inheritance without classes?
- What's the performance impact of deep prototype chains?
- How do you check if a property is inherited or own?
- What's the difference between Object.create() and new?
- How do ES6 classes work with prototypes?
- What's the constructor property and why is it important?
- How would you implement multiple inheritance in JavaScript?