JavaScript Notes
Master JavaScript prototypes with deep explanations of prototype chain, __proto__, Object.getPrototypeOf, prototype property, constructor functions, memory diagrams, and interview Q&A.
What is a Prototype?
In JavaScript, every object has an internal link to another object called its prototype. This prototype object has its own prototype, forming a chain known as the prototype chain. When you access a property on an object, JavaScript first looks at the object itself, then follows the prototype chain until it finds the property or reaches null.
Prototypes are the mechanism by which JavaScript objects inherit features from one another. This is fundamentally different from classical inheritance in languages like Java or C++.
Why Prototypes Matter
- JavaScript is a prototype-based language, not class-based
- All inheritance in JavaScript is built on prototypes
- Even ES6 classes are syntactic sugar over prototypes
- Understanding prototypes is essential for understanding JavaScript objects
The Prototype Chain
Every object in JavaScript has a hidden [[Prototype]] internal property that points to its prototype object.
Accessing the Prototype
const obj = { name: "JavaScript" };
// Three ways to access prototype
console.log(Object.getPrototypeOf(obj)); // Recommended
console.log(obj.__proto__); // Deprecated but common
console.log(obj.constructor.prototype); // Via constructor
// All point to Object.prototype
console.log(Object.getPrototypeOf(obj) === Object.prototype); // true[Object: null prototype] {}
[Object: null prototype] {}
[Object: null prototype] {}
truePrototype Property vs [[Prototype]]
These are two different things:
[[Prototype]](accessed via__proto__orObject.getPrototypeOf) - The internal prototype link every object has.prototypeproperty - A property that only functions have, used when creating instances withnew
Memory Diagram
Constructor Functions and Prototypes
function Animal(name, sound) {
// Instance properties
this.name = name;
this.sound = sound;
}
// Prototype methods (shared across all instances)
Animal.prototype.speak = function() {
console.log(`${this.name} says ${this.sound}`);
};
Animal.prototype.info = function() {
console.log(`I am ${this.name}`);
};
const dog = new Animal("Dog", "Woof");
const cat = new Animal("Cat", "Meow");
dog.speak();
cat.speak();
// Methods are shared (same reference)
console.log(dog.speak === cat.speak); // trueDog says Woof Cat says Meow true
Why Prototype Methods?
Property Lookup (Shadowing)
function Vehicle(type) {
this.type = type;
}
Vehicle.prototype.wheels = 4;
Vehicle.prototype.describe = function() {
return `${this.type} with ${this.wheels} wheels`;
};
const car = new Vehicle("Car");
const bike = new Vehicle("Bike");
bike.wheels = 2; // Creates OWN property (shadows prototype)
console.log(car.wheels); // 4 (from prototype)
console.log(bike.wheels); // 2 (own property)
console.log(car.describe()); // "Car with 4 wheels"
console.log(bike.describe()); // "Bike with 2 wheels"
// Check property ownership
console.log(car.hasOwnProperty("wheels")); // false
console.log(bike.hasOwnProperty("wheels")); // true
console.log(car.hasOwnProperty("type")); // true4 2 Car with 4 wheels Bike with 2 wheels false true true
Property Lookup Algorithm
Object.create()
Create objects with a specific prototype.
const personProto = {
greet() {
return `Hello, I am ${this.name}`;
},
getAge() {
return this.age;
}
};
const alice = Object.create(personProto);
alice.name = "Alice";
alice.age = 30;
const bob = Object.create(personProto);
bob.name = "Bob";
bob.age = 25;
console.log(alice.greet());
console.log(bob.greet());
console.log(Object.getPrototypeOf(alice) === personProto); // trueHello, I am Alice Hello, I am Bob true
Object.create(null)
// Object with NO prototype
const bareObj = Object.create(null);
bareObj.key = "value";
console.log(bareObj.key); // "value"
// console.log(bareObj.toString()); // TypeError - no Object.prototype!
console.log("toString" in bareObj); // falseBuilt-in Prototypes
Built-in Chain Diagram
Modifying Prototypes
Adding Methods to Built-in Prototypes (NOT recommended)
Safe Way: Using Symbols
Prototype Methods Reference
const parent = { role: "parent", greet() { return "Hello"; } };
const child = Object.create(parent);
child.name = "Child";
// Check prototype
console.log(Object.getPrototypeOf(child) === parent); // true
// Set prototype (avoid in performance-critical code)
const newProto = { role: "new parent" };
Object.setPrototypeOf(child, newProto);
console.log(child.role); // "new parent"
// Check if property exists on object itself
console.log(child.hasOwnProperty("name")); // true
console.log(child.hasOwnProperty("role")); // false
// Check if property exists anywhere in chain
console.log("name" in child); // true
console.log("role" in child); // true
// instanceof
function Dog() {}
const rex = new Dog();
console.log(rex instanceof Dog); // true
console.log(rex instanceof Object); // truePrototype Pollution (Security)
Performance Considerations
// DON'T: Modify prototype after object creation
function Bad() {}
const obj1 = new Bad();
Bad.prototype.method = function() {}; // Deoptimizes!
// DO: Set up prototype before creating instances
function Good() {}
Good.prototype.method = function() {};
const obj2 = new Good(); // Optimized
// DON'T: Use Object.setPrototypeOf in hot paths
// It invalidates inline caches and deoptimizes
// DO: Use Object.create for prototype setup
const proto = { shared: true };
const instance = Object.create(proto);Common Traps 🪤
| Trap | Problem | Fix |
|---|---|---|
Reassigning .prototype | Breaks link for existing instances | Modify properties on existing prototype |
Using __proto__ | Deprecated, performance issues | Use Object.getPrototypeOf() |
Modifying Object.prototype | Affects every object — prototype pollution | Never do it |
Forgetting hasOwnProperty | for...in iterates inherited keys too | Always filter with hasOwnProperty |
Checking with in | Finds inherited properties | Use .hasOwnProperty for own-only check |
Interview Questions
Q1: What is the output?
function Foo() {}
Foo.prototype.x = 1;
const a = new Foo();
console.log(a.x);
Foo.prototype = { x: 2 };
console.log(a.x);
const b = new Foo();
console.log(b.x);Answer: 1, 1, 2. Reassigning .prototype doesn't affect existing instances (they still link to old prototype object).
Q2: What is the output?
const obj = {};
console.log(obj.__proto__ === Object.prototype);
console.log(Object.prototype.__proto__);Answer: true, null. Object.prototype is the end of the chain.
Q3: Explain prototype chain for arrays
Answer: [] -> Array.prototype -> Object.prototype -> null. Array methods like push/pop come from Array.prototype, toString/hasOwnProperty from Object.prototype.
Q4: Difference between __proto__ and .prototype?
Answer: __proto__ is the actual prototype link on every object. .prototype is a property only on functions, used as the prototype for instances created with new.
Q5: What is the output?
function A() {}
A.prototype.x = 10;
const a = new A();
A.prototype.x = 20;
console.log(a.x);Answer: 20. Modifying a property on the existing prototype object is reflected in all instances (they share the reference).
Summary
| Concept | Description |
|---|---|
[[Prototype]] | Internal link to parent object |
.prototype | Function property, used with new |
| Prototype chain | Linked list of prototypes ending at null |
| Property lookup | Own → prototype → prototype → ... → null |
Object.create() | Create object with specific prototype |
hasOwnProperty | Check if property is directly on object |
| Shadowing | Own property hides prototype property |
Practice Exercises
- Build a prototype chain with 3 levels
- Implement a polyfill using prototype extension
- Create objects using Object.create with methods
- Demonstrate property shadowing and deletion
- Build a simple class system using only prototypes
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Prototype in JavaScript — Complete Guide with Examples 2026.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, advanced, prototype, prototype in javascript — complete guide with examples 2026
Related JavaScript Master Course Topics