I'm looking for a way to accomplish a certain task and that is, going from
jQuery.when.apply( null, promiseArray ).done(...)
to
when( promiseArray ).done(...)
As you might know, .bind()
can get used to create something like default arguments and also doing some quite nifty stuff. For instance, instead of always calling
var toStr = Object.prototype.toString;
// ...
toStr.call([]) // [object Array]
we can do it like
var toStr = Function.prototype.call.bind( Object.prototype.toString );
toStr([]) // [object Array]
This is fairly cool (even if there is a performance penality invoking .bind()
like this, I know it and I'm aware of it), but I can't really accomplish it for jQuerys .when
call. If you got an unknown amount of promise objects, you usually push those into an array, to then be able to pass those into .when
like in my first code snippet above.
I'm doing this so far:
var when = Function.prototype.apply.bind( $.when );
Now we can go like
when( null, promiseArray ).done(...)
This works, but I also want to get rid of the need to pass in null
explicitly every time. So I tried
var when = Function.prototype.apply.bind( $.when.call.bind( null ) );
but that throws at me:
"TypeError: Function.prototype.apply called on incompatible null"
I guess I'm sitting over this for too long now and can't think straight anymore. It feels like there is an easy solution. I don't want to use any additional function to solve this issue, I'd absolutely prefere a solution using .bind()
.
See a complete example here: http://jsfiddle.net/pp26L/
Call invokes the function and allows you to pass in arguments one by one. Apply invokes the function and allows you to pass in arguments as an array. Bind returns a new function, allowing you to pass in a this array and any number of arguments.
Summary. call : binds the this value, invokes the function, and allows you to pass a list of arguments. apply : binds the this value, invokes the function, and allows you to pass arguments as an array. bind : binds the this value, returns a new function, and allows you to pass in a list of arguments.
The call() and apply() are very similar methods. They both execute the bound function on the object immediately. The bind() method does not execute the function right away. Instead, it creates and returns a bound function that can be executed later.
The bind method creates a copy of the function and sets the this keyword, while the call and apply methods sets the this keyword and calls the function immediately.
This should work:
when = Function.prototype.apply.bind( $.when, null);
You just bind (or curry, if you prefer) the first argument of .bind
and fix it to null
.
Fiddle.
bind
accepts a variable number of arguments, so you can partially apply a method. So, instead of:
var when = Function.prototype.apply.bind( $.when );
Do this:
var when = Function.prototype.apply.bind( $.when , null );
And updated jsfiddle: http://jsfiddle.net/pp26L/2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With