JS: Reflect.apply
(new in ECMAScript 2015)
Reflect.apply(f, this-binding, argArray)-
- Call
fwith this-binding of this-binding, and with arguments of elements in argArray. - Return the result.
- argArray is array or Array-Like Object.
function ff(a, b) { return a + b; } console.assert( Reflect.apply(ff, null, [1, 2]) === 3, ); // apply an arrow function console.assert( Reflect.apply((a, b) => (a + b), null, [1, 2]) === 3, ); // example of Reflect.apply with this-binding argument function ff(x, y) { this.dog = x; this.cat = y; } const jj = {}; Reflect.apply(ff, jj, [3, 4]); console.log(jj); // { dog: 3, cat: 4 } - Call
Function.prototype.apply vs Reflect.apply
- Function.prototype.call and Function.prototype.apply are older.
- Reflect.apply is new.
Function.prototype.apply does not work if a function has a property "apply" or in its parent chain.
// comparison of Reflect.apply vs Function.prototype.apply console.log( Reflect.apply(Math.max, null, [24, 185, 53]), ); // 185 console.log( Math.max.apply(null, [24, 185, 53]), ); // 185