JS: Reflect.apply

By Xah Lee. Date: . Last updated: .

New in ES2015.

Reflect.apply ( f, this_binding, argList )

Call f with its this keyword having value of this_binding, and with arguments of elements in argList.

Return the result.

argList is array object or array-like object.

function f(a, b) { return a + b; }
console.log( Reflect.apply ( f , undefined, [3,4] ) ); // 7

Here's 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 is similar to Function.prototype.apply, but with clean syntax. It does not rely on prototype inheritance.

[see JS: Function.prototype.apply]

Reference

ECMAScript® 2016 Language Specification#sec-reflect.apply

back to JS: Reflect Object

Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth