-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
_.delay has no option to bind "this", and instead binds "this" to called function, which is useless #494
Comments
_.delay = function(func, wait) {
// ...
return setTimeout(function(){ return func.apply(this, args); }, wait);
}; |
Yes, I think that would be more consistent with the rest of the library, and more useful. Thanks |
We can make that change -- but how would that possibly allow the value of |
Erm...whoops. A third |
In that case, I'm going to change it to |
Fixed in: 1366d90 |
Quick note for any future readers who are still unsatisfied with:
There are several alternative options you can use:
That would look something like this (untested) code:
|
I don't understand how we can call this "fixed". The various solutions in the comment above all appear very unreadable to me. The change in 1366d90 simply stops sending an odd context to the called function. Is there a reason |
There's no reason to change _.delay(_.bind(func, context), 500) |
Thanks! I should have tried that I guess, but assumed wrongly based on the complexity of the above answers that something more complicated was needed. |
_.delay is incompatible with _.bind and _.bindAll.
Since _.delay calls its target function "func" with:
func.apply(func, args);
any object that was previously bound to func using _.bind or _.bindAll will not be "this" when func is called. Many other underscore functions have a special parameter for "this".. why not _.delay? Moreover, it seems totally useless to me to have "this" be a reference to the function itself. Why not just call func directly, instead of using "apply", and then not mess with the value of "this"? Am I missing something? Here is code for the _.delay function for reference purposes:
_.delay = function(func, wait) {
return setTimeout(function(){ return func.apply(func, args); }, wait);
};
The text was updated successfully, but these errors were encountered: