JS: Reflect.apply (λ)

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2015)

Reflect.apply(f, thisBinding, argArray)
function ff(a, b) {
 return a + b;
}
console.log(Reflect.apply(ff, null, [1, 2]));
// 3
// apply an arrow function
console.log(Reflect.apply(((a, b) => (a + b)), null, [1, 2]));
// 3
// example of Reflect.apply with thisBinding argument

function ff(a, b) {
 this.k1 = a;
 this.k2 = b;
}

const jj = {};

Reflect.apply(ff, jj, [3, 4]);

console.log(jj);
// { k1: 3, k2: 4 }

Purpose of Reflect.apply

Reflect.apply is created in ECMAScript 2015. It fixes some problem of

because

/*
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

JavaScript. Apply Function