Prototypes
A prototype is another object that your object falls back to when a property is not found on it. JavaScript reuses behaviour by delegating to prototypes rather than by copying members into each instance.
Detailed Explanation
Every JavaScript object holds a hidden link to another object. The specification calls it the internal [[Prototype]] slot. When you read a property, the engine checks the object first; if the property is not there, it follows that link and checks the prototype. The object does not own the property, it borrows it.
This is called delegation, and it is the core difference from class-based languages. In Java or C++, a class describes a layout and each instance gets its own copy of the members. In JavaScript nothing is copied. Ten thousand User instances share one greet function that lives on one prototype object. Change that function at runtime and all ten thousand instances see the change immediately, because they were never holding a copy.
Two things are both called "prototype" and confusing them causes most of the trouble in this area.
- An object's
[[Prototype]]is the hidden link described above. Read it withObject.getPrototypeOf(obj), set it withObject.setPrototypeOf. The legacyobj.__proto__accessor reaches the same slot and is kept alive only for compatibility. - A function's
.prototypeis an ordinary, visible property on the function. It is not that function's own prototype. It is the object that instances will delegate to when you call the function withnew.
So User.prototype is the object, and Object.getPrototypeOf(new User()) returns that same object. One is the template, the other is the live link. Only functions that can be constructed have a .prototype property: normal functions and classes do, while arrow functions, method shorthand, and async functions do not.
The function's .prototype object also carries a constructor property pointing back at the function, which is how instance.constructor works.
There are three common ways to set a prototype. Object.create(proto) makes a new object with the prototype you name. Calling a function with new sets the new object's prototype to the function's .prototype. A class declaration does the same thing with tidier syntax. Object.create(null) is the special case: an object with no prototype at all, which is what you want for a lookup table keyed by untrusted strings, since there is no inherited constructor or toString to collide with.
Prototypes are why JavaScript needs no separate mechanism for built-ins. [].map is not on your array; it is on Array.prototype. "hi".toUpperCase is on String.prototype. The same delegation rule covers everything.
The costs are worth stating. Lookups that miss walk the chain, so a deep chain costs more than a shallow one, though engines cache this aggressively. Object.setPrototypeOf on an existing object is genuinely slow: it invalidates the engine's optimised assumptions about that object's shape and can deoptimise code that touched it. Set the prototype at creation time instead. And mutating a built-in prototype, sometimes called monkey-patching, affects every object in the program including code you did not write, which is why it is discouraged outside of deliberate polyfills.
Code Examples
The two meanings of "prototype", side by side.
// The two meanings of "prototype", side by side.
function User(email) {
this.email = email;
}
// A method placed on the function's .prototype property is shared by instances.
User.prototype.describe = function () {
return `user ${this.email}`;
};
const ada = new User("ada@example.com");
console.log(ada.describe()); // => user ada@example.com
console.log(Object.hasOwn(ada, "describe")); // => false (borrowed, not owned)
// The instance's hidden link IS the function's .prototype object.
console.log(Object.getPrototypeOf(ada) === User.prototype); // => true
console.log(User.prototype.constructor === User); // => true
// Nothing was copied, so a later change is visible on existing instances.
User.prototype.describe = function () {
return `<${this.email}>`;
};
console.log(ada.describe()); // => <ada@example.com>Object.create sets the prototype directly, with no constructor involved.
// Object.create sets the prototype directly, with no constructor involved.
const canFly = {
fly() {
return `${this.name} takes off`;
},
};
const drone = Object.create(canFly);
drone.name = "DJI";
console.log(drone.fly()); // => DJI takes off
// A dictionary with no inherited keys at all.
const counts = Object.create(null);
console.log(Object.getPrototypeOf(counts)); // => null
console.log("toString" in counts); // => false
// On a plain {} this would be true, which breaks naive key checks.
console.log("toString" in {}); // => true
// counts has no inherited toString, so it cannot be printed as a string.
// Use Object.keys/entries or a Map instead of relying on default methods.
// Arrow functions have no .prototype and cannot be used with new.
const Wrong = () => {};
console.log(Wrong.prototype); // => undefinedCommon Gotchas
`Foo.prototype` is not `Foo`'s prototype
`Foo.prototype` is the object that *instances* of `Foo` delegate to. The prototype of the function `Foo` itself is `Function.prototype`. Mixing these up produces methods that exist on the constructor but not on any instance.
Reassigning `Constructor.prototype` breaks earlier instances
objects created before the reassignment keep a link to the old object and lose the new methods. Add to the existing `Constructor.prototype` rather than replacing it, and if you must replace it, restore the `constructor` property yourself.
`Object.setPrototypeOf` is a performance cliff
changing an existing object's prototype forces engines to discard optimised code paths for that object. Create the object with the right prototype using `Object.create` or `new`, rather than fixing it up afterwards.
`__proto__` is legacy but standardised
it is an accessor defined on `Object.prototype` and kept in the specification for web compatibility. Prefer `Object.getPrototypeOf` and `Object.setPrototypeOf` in new code. Note also that `"__proto__"` as a key in parsed JSON is a known prototype pollution vector.