Prototype in JavaScript
A prototype is an object that other objects inherit properties and methods from. Every JavaScript object has a hidden link, called [[Prototype]], to another object. When you ask for a property the object does not have, JavaScript follows that link and looks there instead. This is how arrays get map, strings get toUpperCase, and objects made by a constructor function share the same methods.
The lookup in action
When you write user.greet(), the engine first checks whether user itself has a greet property. If not, it checks the object's prototype, then that prototype's prototype, and so on. This series of links is the prototype chain. It ends at Object.prototype, whose own prototype is null. If the property is not found anywhere, the result is undefined.
function User(name) {
this.name = name; // own property, one per object
}
User.prototype.greet = function () {
return `Hello, ${this.name}`; // shared by all users
};
const u1 = new User('Isha');
const u2 = new User('Dev');
console.log(u1.greet()); // Hello, Isha
console.log(Object.getPrototypeOf(u1) === User.prototype); // true
console.log(Object.hasOwn(u1, 'name')); // true
console.log(Object.hasOwn(u1, 'greet')); // false: inherited
console.log(u1.greet === u2.greet); // true
// Own property shadows the prototype
u2.greet = () => 'Hey!';
console.log(u2.greet()); // Hey!
console.log(u1.greet()); // Hello, Isha (unchanged)
// Plain objects and arrays use the same mechanism
const arr = [1, 2, 3];
console.log(Object.getPrototypeOf(arr) === Array.prototype); // true
prototype versus [[Prototype]]
Two similar names cause most of the confusion here. Every regular function has a property called prototype, an object that will become the [[Prototype]] of instances created with new. The instance itself does not have a prototype property; it has the hidden [[Prototype]] link. To read that link, use Object.getPrototypeOf(obj). The older obj.__proto__ accessor does the same thing but is considered legacy, so prefer the method in new code.
You can also create an object with a chosen prototype directly: Object.create(proto) returns a new empty object whose [[Prototype]] is proto. The next lesson uses this to build inheritance chains.
Why methods go on the prototype
Putting methods on the prototype means one copy of each function serves every instance, saving memory and letting you add or fix a method for all existing objects at once. Instance-specific data stays on the object itself. Writing to a property always creates or changes an own property on the object; it never modifies the prototype. That is why giving u2 its own greet did not affect u1.
A trap: shared mutable values on the prototype
Methods are safe to share, but arrays and objects are not. If you write User.prototype.friends = [], every user shares that single array. Calling u1.friends.push('Sam') modifies the shared array, so u2.friends also contains 'Sam'. The push does not create an own property, because it changes the array rather than assigning to friends. Put per-object data like arrays inside the constructor with this.friends = [].
Also avoid adding methods to built-in prototypes such as Array.prototype in your own projects. It can clash with future language features and with other libraries. Arrow functions are another gotcha: they have no prototype property and cannot be used with new.
Key points
- Missing properties are looked up along the prototype chain.
- Functions have
prototype; instances link to it through[[Prototype]]. - Share methods on the prototype; keep data, especially arrays and objects, on each instance.
- Use
Object.getPrototypeOfandObject.hasOwnto inspect objects.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.