ES6 Reflect API的优点 [英] Benefits of ES6 Reflect API

查看:89
本文介绍了ES6 Reflect API的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力升级一些代码以使用ES6语法。我有以下代码行:

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

删除this._foo;

我的linter提出了使用建议:

and my linter raised a suggestion to use:

Reflect.deleteProperty(this,'_ foo');

您可以找到此方法的文档这里

You can find the documentation for this method here.

MDN文档状态:


Reflect.deleteProperty方法允许您删除
对象的属性。它返回一个布尔值,指示属性
是否已成功删除。它几乎与非严格的
删除运算符相同。

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.

如果我不依赖于成功/失败删除有没有理由支持 Reflect.deleteProperty delete 是非严格的是什么意思?

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?

我觉得很多用例都是 Reflect API用于解决异常情况和/或提供更好的条件流,但代价是更冗长的陈述。我想知道如果我没有遇到当前用法的任何问题,使用 Reflect API是否有任何好处。

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.

推荐答案

反映 API公开了常见的JavaScript习语背后的抽象操作。 主要用于提供合理的方法来转发在代理陷阱上调用的操作。所有 Reflect 方法都匹配具有相同名称的代理陷阱的签名,因此您可以使用 new Proxy(target,Reflect)创建与 target 对象具有相同行为的对象 - 所有内容都将被转发,包括特殊的JavaScript怪癖。

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.

它是对于getter和prototypes尤其重要,如很多方法都是接收者

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


提供这个的价值如果遇到getter则调用target。

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

请考虑以下代码:

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.

这篇关于ES6 Reflect API的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆