JavaScript: Function.prototype.apply
f.apply(obj)
- evaluate
f()
with this Binding having value of obj. f.apply(obj, arg_list)
- elements of the array arg_list is passed to the function as arguments. arg_list is a array or Array-Like Object.
// example of using apply function ff() { return this; } const x2 = {kk:373}; console.log( ff.apply( x2 ) ); // { kk: 373 }
Example with 2 arguments:
// example of using apply function ff(a,b) { this.x = a; this.y = b; } const x3 = {}; ff.apply(x3, [7,8]); console.log( x3 ); // { x: 7, y: 8 }
// Second arg of apply can be Array-Like Object function ff(a,b) { return [this, a, b] } console.log( ff.apply( {}, {0:7,1:8,length:2}) ); // [ {}, 7, 8 ]
Reflect.apply
Tip: you might want to use Reflect.apply instead.