Just because of curiosity I wanted to make a bounded function with this particular approach :
var fn = function(a, b, c) {
return a.call.apply(a.bind, arguments)
}
var boundFn = function(a, b, c) {
fn.apply(null, arguments)
}
function unboundedFn() {
console.log(this, arguments)
}
var boundedFn = boundFn(unboundedFn, x, y);
So I'm trying to understand what a.call.apply(a.bind, arguments) do exactly ?
If You have a function like this:
function unboundedFn() {
console.log(this, arguments)
}
You can use unboundedFn.call(thisObj, arg1, arg2)
or unboundedFn.apply(thisObj, [arg1, arg2])
to run it, but changing what this
means inside. Both call
and apply
do the same, and only difference is way of passing arguments.
Becouse call
, apply
and also bind
are methods, you can run for example unboundedFn.call.call.call.call.apply.apply.apply.apply()
, but it doesn't seem to have to much sense.
In your example return a.call.apply(a.bind, arguments)
is equal to return a.bind.call(...arguments)
, which is equal to a.bind(...arguments.slice(1))
, so whole fn function can be simplified to:
function fn(a,b,...args){
return a.bind(b, ...args);
}