JavaScript Notes
Understand JavaScript prototypes: prototype property, prototype chain, Object.create, adding methods to prototypes, and how ES6 classes map to prototypes. With interview Q&A.
Every JavaScript function has a prototype property — an object that instances created by that function automatically inherit from. This is JavaScript's native mechanism for sharing methods across objects without copying them.
Understanding prototypes is the foundation of everything in JavaScript OOP — classes, inheritance, the prototype chain, this behavior — it all builds on this one concept.
📊 Prototype Relationship Diagram
💻 Example 1 — Adding Methods to a Prototype
Hello, I'm Riya! Hello, I'm Aman! wohotech.in gmail.com true
💻 Example 2 — Prototype vs Own Properties
function Product(name, price) {
this.name = name; // own property
this.price = price; // own property
}
Product.prototype.currency = "INR"; // shared property
Product.prototype.formatted = function() { // shared method
return `${this.currency} ${this.price} — ${this.name}`;
};
const laptop = new Product("Laptop", 75000);
const phone = new Product("Phone", 25000);
console.log(laptop.formatted());
console.log(phone.formatted());
// Own properties
console.log(Object.keys(laptop)); // only own enumerable props
console.log(laptop.hasOwnProperty("name")); // true — own
console.log(laptop.hasOwnProperty("currency")); // false — inherited
console.log(laptop.hasOwnProperty("formatted")); // false — inheritedINR 75000 — Laptop INR 25000 — Phone [ 'name', 'price' ] true false false
💻 Example 3 — How ES6 Classes Map to Prototypes
// ES6 Class version
class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound`; }
}
// Equivalent prototype version
function AnimalOld(name) { this.name = name; }
AnimalOld.prototype.speak = function() { return `${this.name} makes a sound`; };
// They produce the same prototype structure
const a1 = new Animal("Cat");
const a2 = new AnimalOld("Cat");
console.log(a1.speak());
console.log(a2.speak());
// Both methods live on the prototype
console.log(typeof Animal.prototype.speak); // "function"
console.log(typeof AnimalOld.prototype.speak); // "function"
// Class syntax IS prototype-based under the hood
console.log(typeof Animal); // "function" — a class is a function!Cat makes a sound Cat makes a sound function function function
💻 Example 4 — Object.create() for Prototypal Inheritance
// Define base object (serves as prototype)
const personProto = {
greet() { return `Hi, I'm ${this.name}, ${this.age} years old`; },
isAdult(){ return this.age >= 18; }
};
// Create instances using Object.create
const riya = Object.create(personProto);
riya.name = "Riya";
riya.age = 22;
const kid = Object.create(personProto);
kid.name = "Aryan";
kid.age = 12;
console.log(riya.greet());
console.log(kid.greet());
console.log(riya.isAdult());
console.log(kid.isAdult());
// riya and kid SHARE the same prototype object
console.log(Object.getPrototypeOf(riya) === personProto); // true
console.log(Object.getPrototypeOf(kid) === personProto); // trueHi, I'm Riya, 22 years old Hi, I'm Aryan, 12 years old true false true true
💻 Example 5 — Extending Built-In Prototypes (Use with Caution)
436 87.2
⚠️ Warning: Extending native prototypes (Array.prototype,String.prototype) in production code or libraries is dangerous — it can conflict with future ECMAScript additions or other libraries. Use it only in isolated personal/learning projects.
📋 Prototype Methods Quick Reference
| Method | Purpose | Example |
|---|---|---|
Fn.prototype.method = fn | Add shared method | User.prototype.greet = ... |
Object.create(proto) | Create obj with specific prototype | Object.create(protoObj) |
Object.getPrototypeOf(obj) | Get prototype of obj | Object.getPrototypeOf(instance) |
Object.setPrototypeOf(obj, proto) | Change prototype (avoid — slow) | Rarely used |
obj.hasOwnProperty(key) | Check if own property | riya.hasOwnProperty("name") |
proto.isPrototypeOf(obj) | Is proto in obj's chain? | Animal.prototype.isPrototypeOf(dog) |
⚠️ Common Mistakes
❌ Mistake 1 — Defining Methods Inside Constructor (Memory Waste)
function User(name) {
this.name = name;
this.greet = function() { return `Hi, ${this.name}`; }; // ← new function per instance!
}
const u1 = new User("Riya");
const u2 = new User("Aman");
console.log(u1.greet === u2.greet); // false — each has its OWN copy!false
❌ Mistake 2 — Replacing prototype Instead of Adding to It
function Dog(name) { this.name = name; }
Dog.prototype = { bark: function() { return "woof"; } };
// This REPLACES the entire prototype — loses the `constructor` reference
const d = new Dog("Rex");
console.log(d.constructor === Dog); // false — broken!
console.log(d.constructor === Object); // true — unexpected!false true
❌ Mistake 3 — Mutating a Shared Prototype Object Property
[ 'Riya' ]
Fix: Define arrays/objects as own properties in the constructor: this.members = [];🎯 Key Takeaways
- Every function has a
.prototypeobject; instances created withnewinherit from it - Methods on
.prototypeare shared by all instances — stored once, not copied - ES6
classis clean syntax for the same prototype mechanism —typeof MyClass === "function" - Use
hasOwnProperty()to distinguish own properties from inherited ones Object.create(proto)lets you set up prototype chains without constructor functions- Never define methods inside the constructor — put them on
.prototypeto save memory - Avoid replacing the entire
.prototypeobject — it breaks theconstructorreference
❓ Interview Questions
Q1. What is a prototype in JavaScript? > Every JavaScript function has a .prototype property — an object that instances (created with new) automatically inherit from. It stores shared methods and properties that all instances can access without each having their own copy.
Q2. How are methods shared in JavaScript? > Methods defined on Constructor.prototype are shared by all instances through the prototype chain. Each instance has an internal [[Prototype]] link to Constructor.prototype, so when instance.method() is called, JavaScript finds it there.
Q3. What is the difference between own properties and prototype properties? > Own properties are set directly on the instance (usually in the constructor). Prototype properties are on the prototype object, shared by all instances. hasOwnProperty("key") returns true only for own properties.
Q4. Why put methods on the prototype and not inside the constructor? > Methods inside the constructor are recreated for each instance — wasting memory. Methods on the prototype are created once and shared. For 1000 instances, prototype sharing means 1 function object; constructor methods means 1000.
Q5. What does Object.create(proto) do? > Creates a new plain object whose [[Prototype]] is explicitly set to proto. It does not call a constructor function. Useful for manual prototype chain setup or creating objects with a specific prototype without classes.
Q6. If I add a method to Constructor.prototype after creating instances, can they use it? > Yes! Because instances hold a *reference* to the prototype, not a copy. Adding to the prototype after instance creation makes the new method immediately available on all existing instances.
Q7. What happens to the constructor property when you replace .prototype? > When you do MyFn.prototype = { ... }, you replace the default prototype object (which had constructor: MyFn) with a plain object. The constructor property is lost — new instances.constructor now points to Object instead of MyFn. Always add constructor: MyFn back if you must replace the prototype.
Q8. How does a JavaScript class relate to prototypes? > class is syntactic sugar over constructor functions + prototype assignment. The class body methods are automatically placed on ClassName.prototype. typeof MyClass === "function" and MyClass.prototype contains all the instance methods. Classes also add strict rules (no calling without new, no hoisting) on top.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Prototypes – Prototype-Based Inheritance, Object.create & prototype Property.
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, oop, prototypes, javascript prototypes – prototype-based inheritance, object.create & prototype property
Related JavaScript Master Course Topics