Introduction
Let's dive into the use of method synthesis, the Proxy, and the Reflect class.
We'll cover the following
The power of method synthesis
Method synthesis is the most powerful form of metaprogramming.
- You can alter the API and the behavior of objects based on the runtime context and state of the objects.
- You can take existing classes and twist and turn them the way it makes sense to meet your needs, far beyond what was intended by the author of a class.
✏️ Method synthesis was not possible until very recently in JavaScript. In the past, all you could do was method injection, which you saw in the previous chapter, Exploring Metaprogramming.
-
With the introduction of method synthesis and two new classes—
Reflect
andProxy
—you can intercept method invocations right in the middle of their execution and change the fate of the calls, the way you like. -
With the ability to synthesize members at runtime, different objects of a class may entertain different methods and behavior.
-
While this is highly complex programming, it provides phenomenal flexibility to alter the behavior of objects, and it makes the code incredibly extensible.
Get hands-on with 1400+ tech skills courses.