Observe
Observe this function constructed with Function.apply.bind
:
1 | const f = Function.apply.bind( |
f will take an array of length two and log both elements:
1 | f([1,2]) // 1, 2 |
What does bind do?
Function.prototype.bind
will bind the first argument to the function it is called on as this
, and the rest of the arguments will precede any provided when the new function is called.
So the original block is equivalent to calling Function.apply
on the function function(x,y){console.log( x, y );}
, and the first argument is null:
1 | const f = (argsArray) => (function(x,y){console.log(x,y)}).apply(null, argsArray) |
What does apply do?
The apply() method calls the specified function with a given this value, and arguments provided as an array (or an array-like object).
So the above statement can be further simplified to:
1 | const f = (argsArray) => {console.log(argsArray[0], argsArray[1])} |