O'Reilly logo

Rediscovering JavaScript by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Metaprogramming with Proxy

In an earlier example, in Injecting Multiple Properties, we introduced a few missing properties like first and last into arrays. That’s member injection—we knew what to introduce or inject at code writing time. While that’s fun and useful, member synthesis ups the challenge and the resulting benefits by a few notches. With synthesis we can bring on board new members into a class based on runtime context. We can also alter the behavior of existing methods or properties dynamically based on the current state at runtime. To achieve this, we need the help of the Proxy class. We’ll first explore this newly added class in JavaScript and then employ it for method synthesis.

The Proxy Class

An instance of the Proxy class ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required