JavaScript Prototypes

By admin , 22 July, 2026

Do you know what is prototype, . Don't worry we'll cover everything, just keep reading techbrushup.

In JavaScript, a prototype is essentially a "fallback object" that other objects can inherit properties and methods from.

Every JavaScript object has an internal link to another object called its prototype. This prototype object can have its own prototype, forming a prototype chain.

When you try to access a property or method on an object, JavaScript first looks for it on the object itself. If it doesn't find it, it looks at the object's prototype (the fallback). If it's not there, it looks at that prototype's prototype, and so on. This chain continues until it reaches null—this is called the prototype chain.

prototype.js
			
// Constructor function
function Person(name) {
    this.name = name;
}

// Adding a method to the prototype
Person.prototype.sayHello = function() {
    console.log(`Hello, my name is ${this.name}`);
};

const alice = new Person("Alice");
const bob = new Person("Bob");

alice.sayHello(); // Hello, my name is Alice
bob.sayHello();   // Hello, my name is Bob

// Both share the same sayHello method from Person.prototype
console.log(alice.sayHello === bob.sayHello); // true
			
			

Why is Prototype used? (The 2 Big Reasons)

  • Memory Efficiency: If you create 10,000 Person objects, JavaScript does not copy the sayHello function into each of them. Instead, all 10,000 instances simply link to the single sayHello function living on Person.prototype. This saves massive amounts of memory..
  • Dynamic Updates: If you create 10,000 Person objects, JavaScript does not copy the sayHello function into each of them. Instead, all 10,000 instances simply link to the single sayHello function living on Person.prototype. This saves massive amounts of memory..
prototype.js
			
Person.prototype.sayGoodbye = function() {
  console.log("Goodbye!");
};
alice.sayGoodbye(); // Works perfectly, even though it was added after 'alice' was created!
			
			

Next.....


Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.