JS: Reflect.apply

By Xah Lee. Date: . Last updated: .

New in ES2015.

Reflect.apply ( f, this_binding, arg_list )

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

Return the result.

[see JS: “this” Binding]

arg_list is array or array-like object.

[see JS: Array-Like Object]

function f(a, b) { return a + b; }
console.log( Reflect.apply ( f , null, [1,2] ) === 3 ); // true

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 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 JS: Function.prototype.apply]

back to JS: Reflect

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials