METAPROGRAMMING IN JAVASCRIPT
Introduction to “Reflect” API for Metaprogramming in JavaScript
In this lesson, we are going to learn about the `Reflect` global object available in the ES2015+. It provides various static functions to introspect and modify JavaScript objects.
As we learned from the earlier lesson, reflection represents introspection, intercession, and modification of the program. Prior to ES2015 (ES6), we had a few tools available to us to introspect and modify the behavior of the program such as Object.keys
or instanceof
operator among many.
In ES2015, we received a Reflect
global object that provides some pretty useful methods for metaprogramming. Like Math
and JSON
objects, Reflect
is not a function nor it’s constructible. Its only job is to provide static methods for reflection. These methods can be divided into two categories.
Introspection methods are non-destructive methods. They are only used to introspect objects. Modification methods are destructive since they mutate the object or its behavior. Most of these methods are derived from old JavaScript implementations and we will talk about this as well.