jQuery followup

January 23, 2009

One point I ought to have made and which a reader pointed out is that jQuery does have plugins—lots of them in fact—which provide some of Protoype's functionality. Also, it's easy enough to write your own plugins if you wish to. I noted the lack of a curry function in my last post, so here's a quick-and-dirty port to jQuery:


jQuery.curry = function(fn) {
    if (arguments.length < 2) return fn;
    args = $.makeArray(arguments).slice(1, arguments.length);
   return function() {
        return fn.apply(this, args.concat($.makeArray(arguments)));
    }
}   
// Use like this:
add = function(n1, n2) { return n1 + n2; }
addThree = $.curry(add, 3);
addThree(2);

The other way of doing this (which I think is more idiomatic) is by modifying Function.prototype directly so you can call a curry method directly on your function. Of course then it's no longer strictly a jQuery plugin.

Tagged with: , , .

Leave a comment:

Comments are closed for this entry.