Visualising Prototypes
Prototypes are easier to follow once you can see the three objects involved and the links between them. For a constructor function Vehicle and an instance van, they are:
van, the instanceVehicle, the constructor functionVehicle.prototype, the object that every instance inherits from
van links to Vehicle.prototype as its prototype. Vehicle holds a reference to Vehicle.prototype in its prototype property, and Vehicle.prototype points back to Vehicle through its constructor property.
van ──[[Prototype]]──▶ Vehicle.prototype ◀──prototype── Vehicle Vehicle.prototype ──constructor──▶ VehicleChecking the triangle
Section titled “Checking the triangle”Each link can be checked in code:
function Vehicle() {}const van = new Vehicle();
console.log(Object.getPrototypeOf(van) === Vehicle.prototype);console.log(Vehicle.prototype.constructor === Vehicle);console.log(van.constructor === Vehicle);console.log(Object.hasOwn(van, 'constructor'));This prints:
truetruetruefalsevan.constructor works even though van has no constructor property of its own, because the lookup finds it one step up, on Vehicle.prototype.
Printing a chain
Section titled “Printing a chain”A short helper can walk the chain with Object.getPrototypeOf() and name each step after its constructor, which shows the chains behind some built-in values:
function chainOf(obj) { const names = []; let proto = Object.getPrototypeOf(obj); while (proto !== null) { names.push(`${proto.constructor.name}.prototype`); proto = Object.getPrototypeOf(proto); } names.push('null'); return names.join(' → ');}
console.log(chainOf({}));console.log(chainOf([1, 2]));console.log(chainOf(() => {}));console.log(chainOf(new Date()));console.log(chainOf(new Map()));This prints:
Object.prototype → nullArray.prototype → Object.prototype → nullFunction.prototype → Object.prototype → nullDate.prototype → Object.prototype → nullMap.prototype → Object.prototype → nullEvery chain ends at Object.prototype and then null. An array gets push() and map() from Array.prototype, and a function gets call() and bind() from Function.prototype. The helper assumes that each prototype has a constructor, which is true of the built-in ones but not of an object made with Object.create(null).
Adding a level
Section titled “Adding a level”To make one constructor’s instances inherit from another, link the two prototype objects. After that, a Bicycle instance finds methods on Vehicle.prototype as well:
function Vehicle() {}Vehicle.prototype.move = function () { return 'moving';};
function Bicycle() {}Object.setPrototypeOf(Bicycle.prototype, Vehicle.prototype);
const bike = new Bicycle();console.log(chainOf(bike));console.log(bike.move());console.log(bike instanceof Vehicle);With the same chainOf() helper, this prints:
Bicycle.prototype → Vehicle.prototype → Object.prototype → nullmovingtrueinstanceof answers exactly this question: whether Vehicle.prototype appears anywhere in the chain of bike.
The same picture with classes
Section titled “The same picture with classes”class Bicycle extends Vehicle builds the same links for you, and it adds one more, from Bicycle itself to Vehicle, so that static methods are inherited too:
class Vehicle {}class Bicycle extends Vehicle {}
console.log(Object.getPrototypeOf(Bicycle.prototype) === Vehicle.prototype);console.log(Object.getPrototypeOf(Bicycle) === Vehicle);console.log(Object.getPrototypeOf(Vehicle) === Function.prototype);This prints:
truetruetrueIn a browser’s developer tools, logging an object and expanding its [[Prototype]] entry shows the same chain, one level at a time.
Steve Griffith covers this topic in his video Visually Understanding JavaScript Prototypes.