Skip to content

Instantly share code, notes, and snippets.

@appden
Created April 14, 2009 19:36
Show Gist options
  • Save appden/95379 to your computer and use it in GitHub Desktop.
Save appden/95379 to your computer and use it in GitHub Desktop.
Class.Mutators.BindAll = function(self, bool) {
if (!bool) return self;
var init = self.initialize;
var exclude = arguments.callee.exclude;
self.initialize = function() {
for (var method in this) {
if (typeof this[method] != 'function' || exclude.contains(method))
continue;
var unbound = this[method];
this[method] = unbound.bind(this);
this[method].__parent = unbound.__parent;
}
return init ? init.apply(this, arguments) : this;
};
return self;
};
Class.Mutators.BindAll.exclude = ['constructor', 'initialize', 'parent'];
/*
Please see for more info:
http://appden.com/javascript/bindall-class-mutator-for-saving-your-sanity/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment