Prototype Methods vs Instance Methods

Putting a method on a prototype allows all instances sharing that prototype to access the same function. Defining a function separately on every instance creates separate function objects and can increase memory usage.

This distinction matters when creating large numbers of objects or designing reusable object abstractions.

Shared Prototype Method
function User(name) {
  this.name = name;
}

User.prototype.greet = function () {
  return `Hello ${this.name}`;
};

const a = new User('A');
const b = new User('B');

console.log(a.greet === b.greet);
💡 Both instances resolve greet through User.prototype, so they reference the same function.
Key Takeaways
  • Prototype methods can be shared by instances.
  • Instance-created methods create separate function objects.
  • Prototype delegation can reduce duplication.
Knowledge Check

Why can prototype methods be memory-efficient for many instances?