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

You can compare the definition of Function.prototype.apply and Reflect.apply in the spec.

Basically they are equivalent, but there is a difference: if the arguments list is null or undefined, Function.prototype.apply will call the function with no arguments, and Reflect.apply will throw.

function func() {
  return arguments.length;
}
func.apply(void 0, null); // 0
Reflect.apply(func, void 0, null); // TypeError: null is not a non-null object

Another difference is that, when you use func.apply, you assume

  • func is a Function instance, i.e. it inherits from Function.prototype
  • func has no apply own property which would shadow Function.prototype.apply

But Reflect.apply doesn’t require that. For example,

var obj = document.createElement('object');
typeof obj; // "function" -- can be called
obj.apply; // undefined -- does not inherit from Function.prototype
Reflect.apply(obj, thisArg, argList); // -- works properly
var func = a => a;
func.apply = a => 0;
func.apply(void 0, [123]); // 0 -- Function.prototype.apply is shadowed by an own property
Reflect.apply(func, void 0, [123]); // 123 -- works properly

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)