Skip to content

Instantly share code, notes, and snippets.

@mattmccray
Created May 26, 2010 16:25
Show Gist options
  • Select an option

  • Save mattmccray/414704 to your computer and use it in GitHub Desktop.

Select an option

Save mattmccray/414704 to your computer and use it in GitHub Desktop.
Class.extend({
callback: function() {
if(arguments.length == 0 || typeof arguments[0] != "string") { throw "You must specify a method name (as a String)"; };
var self = this,
args = Array.prototype.slice.call(arguments),
name = args.shift(),
meth = self[name];
if (typeof meth === 'function') { return function curriedMethod() {
return meth.apply(self, args.concat(Array.prototype.slice.call(arguments)));
};
} else {
throw "Method "+ name +" not found!";
};
}
});
// Usage
var Dog = new Class({
bark: function(event) {
// Method body here...
}
});
var myDog = new Dog();
$('element').on('click', myDog.callback('bark'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment