Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of ES6 Reflect API

I've been working on upgrading some code to use ES6 syntax. I had the following line of code:

delete this._foo;

and my linter raised a suggestion to use:

Reflect.deleteProperty(this, '_foo');

You can find the documentation for this method here.

The MDN docs state:

The Reflect.deleteProperty method allows you to delete a property on an object. It returns a Boolean indicating whether or not the property was successfully deleted. It is almost identical to the non-strict delete operator.

I understand that the delete keyword does not return a value indicating success, but it is much less verbose.

If I'm not dependent on the success/failure of delete is there any reason to favor Reflect.deleteProperty? What does it mean that delete is non-strict?

I feel like a lot of the use cases for the Reflect API are for resolving exceptional cases and/or providing better conditional flow, but at the cost of a much more verbose statement. I'm wondering if there's any benefit to use the Reflect API if I'm not experiencing any issues with my current usages.

like image 481
Sean Anderson Avatar asked Feb 08 '16 18:02

Sean Anderson


People also ask

Why do we need reflect in JavaScript?

The primary use case of the Reflect object is it to make it easy to interfere functionality of an existing object with a proxy and still provide the default behavior. You can always just use the defined counterpart to the Proxy handler in the Reflect object and can be sure that the default behavior stays the same.

What is ES6 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.

What is reflect API in JavaScript?

The use of Reflect API in JavaScript enables you to invoke Object methods, get and set object property values, construct objects, extend and manipulate its properties at run-time. It also permits the creation of dynamic code-handling frameworks and programs.


1 Answers

Reflect API exposes abstract operations staying behind common JavaScript idioms. It's main use to provide reasonable way to forward actions called on Proxy traps. All of Reflect methods match signature of proxy traps with the same name, so you can use new Proxy(target, Reflect) to create object with identical behaviour as target object - everything will be forwarded, including special JavaScript quirks.

It's especially important for getters and prototypes, as third argument of many methods is "receiver"

The value of this provided for the call to target if a getter is encountered.

Consider following code:

var target = {
    get foo() {
        return this.bar;
    },
    bar: 3
};
var handler = {
    get(target, propertyKey, receiver) {
        if (propertyKey === 'bar') return 2;

        console.log(Reflect.get(target, propertyKey, receiver)); // this in foo getter references Proxy instance; logs 2
        console.log(target[propertyKey]); // this in foo getter references "target" - logs 3
    }
};
var obj = new Proxy(target, handler);

When you write Proxy, you expect it to fully cover target object - and there is no idiomatic way to do this without Reflect.

Moreover, having operators as functions is convenient for functional style programming.

like image 155
Ginden Avatar answered Oct 07 '22 21:10

Ginden