JavaScript: Reflect.apply
New in JS2015.
Reflect.apply(f, this_binding, arg_list)
- Call
f
with this Binding of this_binding, and with arguments of elements in arg_list. Return the result. arg_list is array or Array-Like Object.
function f(a, b) { return a + b; } console.log( Reflect.apply ( f , null, [1,2] ) === 3 ); // true
Here is example with this_binding:
// example of Reflect.apply with thisBinding argument function g (a, b) { this.x = a; this.y = b; } const obj = {}; Reflect.apply ( g , obj, [3,4] ); console.log( obj ); // { x: 3, y: 4 }
Reflect.apply vs Function.prototype.apply
Reflect.apply
is similar to
Function.prototype.apply
,
but with clean syntax. It does not rely on prototype inheritance.
Compare:
Reflect.apply(f , obj, arg_list)
vs
f.apply(obj, arg_list)
The
f.apply
may not work if f has a property key "apply"
.
[see Function.prototype.apply]