liveBook · Manning

Reflection

As explained in The Joy of JavaScript

A programming paradigm in which a program can inspect or modify its own structure and behavior.

Reflection lets JavaScript inspect or influence the runtime representation of objects and their behavior. It belongs to metaprogramming: using code to automate, alter, or extend other code. JavaScript supports both static hooks through symbols and dynamic hooks through Proxy and Reflect. section 7.0 section 7.8

Reflection appears in two complementary forms:

  • Static introspection adds symbols as object properties. Custom symbols create unique, collision-free extension points, while well-known symbols hook into operations such as iteration, spreading, primitive conversion, and printing. section 7.6 section 7.5
  • Dynamic introspection changes behavior externally through Proxy and Reflect. These APIs intercept method calls or property access without adding injectable concerns to the target object's interface. section 7.6 section 7.8
  • The separation is architectural: static extension changes an object's shape during construction, whereas dynamic weaving changes behavior during object use. section 7.6

A proxy as a reflective boundary

A proxy stands between client code and a target object. When a property is requested, the proxy can supply its own property or consult the target, making the boundary useful for logging, caching, masking, filesystem access, and network communication. The same mechanism supports method decorators and other cross-cutting behavior without polluting the wrapped object's interface. section 7.6 section 7.8

Figure 7.2 The Proxy pattern uses an object (proxy) to act on behalf of a target. When fetching for a property, if it finds the property in the proxy, the code uses that property; otherwise, it consults the target object. Proxies have lots of uses, including logging, caching, and masking.

Use the proxy diagram to trace how a client accesses a proxy first and how the proxy delegates to its target. section 7.6

How The Joy of JavaScript covers it

How deep it goes
Distinguishes symbol-based hooks from Proxy and Reflect weaving, but does not detail individual trap implementations or complete API reference.
Examples
Uses symbols, well-known symbols, proxy delegation, logging, caching, masking, network communication, and filesystem access as examples.
What it assumes
Builds on treating functions and modules as data, while introducing code as data as the broader metaprogramming model.
What you can do after
Apply it via JavaScript symbols, Proxy, and Reflect to weave behavior, define runtime hooks, and modularize cross-cutting logic.
How it connects
Connects reflection with metaprogramming, symbols, Proxy, Reflect, decorators, cross-cutting concerns, and dynamic extension.

Also explained in

Compare how 13 books explain reflection →

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.