Created
May 26, 2010 16:25
-
-
Save mattmccray/414704 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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