Purpose of Reflect: Invoke Functions and Access Prototypes
Explore the purposes of JavaScript's Reflect class, including invoking functions with Reflect.apply and accessing or modifying object prototypes. Understand how Reflect works with Proxy for cleaner and more flexible metaprogramming in modern JavaScript.
We’ve used a few methods of Reflect so far in this course. Now, it’s time to step back and take a fresh look at this class.
Purpose of Reflect
Reflect has two main purposes:
- It’s a go-to place for various meta-operations on objects. For example,
Reflectprovides methods to get and set an object’s prototype and to check whether a property exists in an object, just to mention a couple. - The
Proxyclass, which you’ll see soon, routes its methods to methods ofReflectby default. Then, when using a proxy, we can override only select operations and conveniently leave the rest to the default implementations.
There are about two dozen methods in Reflect.
Let’s sample a few interesting and often used methods ...