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 or array-like object.

[see JS: “this” Binding]

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, [args] )

vs

f.apply( obj, [args] )

The f.apply may not work if f has a property key "apply".

[see JS: Function.prototype.apply]

Reference

ECMAScript® 2016 Language Specification#sec-reflect.apply

back to JS: Reflect

Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

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

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Reference
  5. DOM
  6. SVG
  7. Web Dev Blog