I am just wondering if there is any good reason to call:
Reflect.apply(myFunction, myObject, args);
instead of:
myFunction.apply(myObject, args);
I am just wondering if there is any good reason to call:
Reflect.apply(myFunction, myObject, args);
instead of:
myFunction.apply(myObject, args);
One use i can think of is using Reflect.apply in flow managment or in functions that execute an array of function
which much more convinient then
since you have more control.
You can compare the definition of
Function.prototype.apply
andReflect.apply
in the spec.Basically they are equivalent, but there is a difference: if the arguments list is
null
orundefined
,Function.prototype.apply
will call the function with no arguments, andReflect.apply
will throw.Another difference is that, when you use
func.apply
, you assumefunc
is aFunction
instance, i.e. it inherits fromFunction.prototype
func
has noapply
own property which would shadowFunction.prototype.apply
But
Reflect.apply
doesn't require that. For example,See also the SO question What does the Reflect object do in JavaScript?, which includes this text in the top answer:
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.