Is there any benefit to call Reflect.apply() over Function.prototype.apply() in ECMAScript 2015?

后端 未结 3 1142
-上瘾入骨i
-上瘾入骨i 2021-02-06 23:32

I am just wondering if there is any good reason to call:

Reflect.apply(myFunction, myObject, args);

instead of:

myFunction.appl         


        
3条回答
  •  醉梦人生
    2021-02-07 00:05

    See also the SO question What does the Reflect object do in JavaScript?, which includes this text in the top answer:

    Now that we have modules, a “@reflect” module is a more natural place for many of the reflection methods previously defined on Object. For backwards-compatibility purposes, it is unlikely that the static methods on Object will disappear. However, new methods should likely be added to the “@reflect” module rather than to the Object constructor

    My understanding is that in previous iterations of JS, tools related to "reflection" have been scattered around the language, as part of the Object prototype and Function prototype. The Reflect object is an effort to bring them under one roof.

    So, in the case of your question, although there are differences (see Oriol's answer), the reason for both to exist is a general move to future-proof reflection tooling in the ES spec.

提交回复
热议问题