52 points · by bensummers · 17y ago · 15 comments
>> #2: Goal: To be able to understand this function:
idk, I find all the js library bind() functions to be a bit yucky. Closures aren't hard:
onclick = function(c){return function() { c.doSomething(stuff,theOtherStuff);}}(this);
Also, they're far more general when you write them like that, and you're not asking the runtime to constantly call array methods etc
Useful resource though, but don't use a js lib :)
Also, I can live with defining such an argument on initial definition, as with Python’s self in method definitions, but having to add an extra argument to every function/method invocation for the execution scope, that must be passed in every time explicitly or everything breaks, is really obnoxious. You could certainly make up a convention for the proper name, but just using this is more straight-forward, and doesn’t really have any particularly significant downsides.
There’s a reason that pretty much every JavaScript framework includes a bind function/method, and that they’re adding it to the official language spec: it’s a convenient and effective pattern.
Plus, the jQuery "bind" function is necessary so you can keep chaining methods.
function _bind(o, m) {
var bound_arguments = Array.prototype.slice.apply(arguments, [2]);
return function() {
var now_arguments = Array.prototype.slice.apply(arguments);
var args = bound_arguments.slice().concat(now_arguments);
return m.apply(o, args);
}
}I guess it's a matter of taste etc.
http://matt.might.net/articles/implementation-of-recursive-f...