liveBook · Manning

Prototype

As explained in JavaScript in Depth

An object-linked reference target from which another object can inherit properties through delegation in JavaScript.

A prototype is an object-linked reference target used for property delegation. If an object lacks a property, JavaScript follows its [[Prototype]] reference, continuing up the chain until it finds the property or reaches null. The relationship is live: several objects can share one prototype, and later prototype changes become visible to all of them. section 5.3 section 5.3.1

Use these rules to reason about prototype behavior:

  • Read operations search the object first, then its prototype chain; writes usually create an own property, which shadows a prototype data property. section 5.3.1
  • Accessor properties change assignment behavior: a prototype setter can receive the assignment while operating with this bound to the receiving object. section 5.3.1 section 5.3.2
  • Inspect precisely with Object.getPrototypeOf(), test chain membership with in, and test direct ownership with Object.hasOwn(); truthiness does not establish property existence. section 5.3.2
  • Object.create() selects a prototype directly, while Object.setPrototypeOf() changes an object's linkage; ordinary chains commonly end at Object.prototype and then null. section 5.3 section 5.3.1

A shared, mutable prototype

const drawerPrototype = { open() { console.log('Opening drawer'); } }; const drawer1 = Object.create(drawerPrototype); const drawer2 = Object.create(drawerPrototype); drawerPrototype.close = function() { console.log('Closing drawer'); }; drawer1.close(); drawer2.close(); Both drawers resolve close through the same live prototype rather than receiving copied methods.

What new performs

new creates an object, links it to the constructor's prototype, calls the constructor with this bound to that object, and returns the constructor's object result when one is supplied. This makes constructor-based prototype linkage a sequence of observable operations rather than a mysterious instance step. section 6.1.1

How JavaScript in Depth covers it

How deep it goes
Traces [[Prototype]] lookup, shadowing, accessors, inspection APIs, and new's four operations, while stopping short of runtime optimization internals.
Examples
Uses shared drawer objects, shadowed labels, accessor properties, prototype inspection, and a simulated new implementation.
What it assumes
Assumes JavaScript objects and properties, while building the lookup chain and constructor linkage from concrete examples.
What you can do after
Diagnose prototype-chain behavior with Object.create(), Object.getPrototypeOf(), in, Object.hasOwn(), and an explicit model of new.
How it connects
Connects prototype inheritance with property descriptors, accessors, this binding, Object.prototype, constructors, and the new operator.

Also explained in

Compare how 5 books explain prototype →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.