Skip to content

Instantly share code, notes, and snippets.

@patrixd
Last active December 31, 2015 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrixd/8025952 to your computer and use it in GitHub Desktop.
Save patrixd/8025952 to your computer and use it in GitHub Desktop.
BindAll from underscore that allows 1 argument to bind all the functions from the prototype, or if there are more arguments they will be the only binded
//bindAll from underscore that allows 1 argument to bind all the functions from the prototype,
//or if there are more arguments they will be the only binded
_.originalBindAll = _.bindAll;
_.bindAll = function (that) {
var funcs = Array.prototype.slice.call(arguments, 1),
validKeys = [], fn;
if (funcs.length == 0) {
for (var i in that) {
fn = that[i];
if (fn && typeof fn == "function" && (!fn.prototype || _.keys(fn.prototype).length == 0))
validKeys.push(i);
}
_.originalBindAll.apply(_, [that].concat(validKeys));
}
else
_.originalBindAll.apply(_, arguments);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment