liveBook · Manning

Prototype pollution

As explained in The Joy of JavaScript

A vulnerability or failure mode in which modifying a prototype affects many objects that inherit from it.

Prototype pollution occurs when changing a base prototype changes the behavior of many objects that inherit from it. Property lookup traverses the prototype hierarchy, so a modification can ripple through derived objects at runtime. section 1.2

Student and Person

If Student objects inherit from Person, a change to Person can affect every Student, even when those objects were already created. The prototype hierarchy saves memory but makes downstream behavior harder to isolate. section 1.2

Common design responses expose different trade-offs:

  • Copy properties into new objects or use mixins to reduce unintended downstream changes; accept a larger memory footprint because each object stores another copy. section 1.2
  • Use constructors or classes behind factory functions and encapsulation boundaries rather than exporting mutable object literals directly. section 2.3
  • Use class and extends syntax when a prototype chain is desired, while recognizing that classes remain syntactic sugar over constructor functions. section 2.3

Encapsulated transaction state

A Transaction class keeps fee data in a private field, while HashTransaction inherits public behavior. Private access such as tx.#feePercent throws a SyntaxError, limiting accidental external changes.

How The Joy of JavaScript covers it

How deep it goes
Explains prototype lookup, runtime ripple effects, and composition trade-offs, then stops short of a general mitigation checklist.
Examples
Uses Student–Person hierarchies, copied object properties, Transaction classes, and private fields to expose the behavior.
What you can do after
Evaluate prototype reuse against object composition and encapsulation when designing JavaScript object models.
How it connects
Connects prototype pollution with differential inheritance, property lookup, mixins, object composition, classes, and encapsulation.

Related concepts

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