I saw this shortcut given as an answer on a code Kata but I am having difficulty understanding exactly what the below example is doing.
function func(fn) {
return fn.bind.apply(fn, arguments);
}
So far my understanding is that bind creates a new function similar to doing the following:
function func(fn) {
return function () {
return fn.apply(fn, arguments);
};
}
Is this the case? Any clearer answers or breakdowns of what is going on would be great.
is just
So we're applying bind to
fn, returningSo the bound function is called with arguments being the arguments of
funcafterfn.Another way to write it would have been:
The fact that the context of the call is the initial function (
arguments[0]) is most certainly only a side effect. The important thing is we wrap the arguments with the function, but make it possible to dynamically pass other arguments.Example 1, wrapping all arguments :
Exemple 2, currying: