ES6 - Reflect API

ES6 introduces new features around meta-programming which involves inspecting or modifying the structure of the program, or changing the way things work in the language itself.

Following are the three forms of meta programming −

  • Introspection − Introspection means a program gathering information about itself. Some examples of JavaScript operators that are used for introspection are typeof, instanceof etc.

  • Self-modification − Self-modification refers to modifying the structure of a program at runtime. It involves accessing or creating new properties at runtime. In other words, self-modification is when some code modifies itself.

  • Intercession − refers to code modifying the default behavior of a programming language. Intercession involves modifying semantics of the programming language or adding new constructs to the program at runtime.

ES6 introduces Reflect Application Programming Interface (Reflect API) and Proxy API that supports meta programming.

Meta Programming with Reflect API

Reflect API in ES6 allows us to inspect, or modify classes, objects, properties, and methods of a program at runtime. The Reflect API provides global Reflect object which has static methods that can be used for introspection. These methods are used to discover low level information about the code. The Reflect API can be used to build automation testing frameworks that examine and introspect program at runtime.

Some commonly used methods of the Reflect object are given below −

Sr.No Method & Description
1 Reflect.apply()

Calls a target function with arguments as specified by the args parameter

2 Reflect.construct()

Equivalent to calling new target(...args) objects of a class

3 Reflect.get()

A function that returns the value of properties.

4 Reflect.set()

A function that assigns values to properties. Returns a Boolean that is true if the update was successful.

5 Reflect.has()

The in operator as function. Returns a Boolean indicating whether an own or inherited property exists.