Skip to content

Instantly share code, notes, and snippets.

@tauren
Created May 1, 2012 19:24
Show Gist options
  • Save tauren/2570687 to your computer and use it in GitHub Desktop.
Save tauren/2570687 to your computer and use it in GitHub Desktop.
// Bind all of an object's methods to a specific context. Useful for ensuring that
// all callbacks defined on an object belong to it.
var context = function(obj, ctx) {
var funcs = Array.prototype.slice.call(arguments, 2);
if (funcs.length == 0) funcs = _.functions(obj);
_.each(funcs, function(f) { obj[f] = _.bind(obj[f], ctx); });
return obj;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment