Ajax Bestiary: A Javascript Field Guide
 
Ajax Bestiary: A Javascript Field Guide
 
 

A Fast & Easy Prototype Based Curry

Posted by Don Albrecht

in Javascript the Good Parts, Douglas Crockford presents a simple method to curry functions.  It’s a great tool that allows you to build functions with arguments predefined in them dynamically.  In his implementation Array.splice & an indirect method declaration is used.  In my version, I use prototype’s enhanced Array object & a direct call to the Function object to the same effect.

Function.curry = function() {
var args = $A(arguments), that = this;
return function() {
return that.apply( null, args.concat(arguments));
};
};

I’ve been using this method for a few months now in both its prototype & jquery forms and find it invaluable, especially when faced with a need to manipulate a large number of similar objects.

  • No Related Post

Leave a Reply