Joined September 2011
·

Anton Kotenko

Java / Web Developer at animatron
·
Munich, Germany
·
·
·

Don't you ever thought about using bind to "hold over" a function call and make your code look nice w/o any libs. Except using an internal Function.bind() (not in all browsers), you may write a 5-liner function like this one, the one which will "defer" calls to the function it wraps:

function def(f) {
    return function() {
        return (function(f, args) {
            return function() { return f.apply(null, args); };
        })(f, arguments);
    };
}

It will allow you to write something like this:

function logThis(arg) {
   console.log(arg);
}
logThis = def(logThis);

function queue() {
   var fs = arguments;
   for (var i = 0, il = fs.length; i < il; i++) {
      fs[i]();
   }
}

queue(logThis('one'), logThis('two'), logThis('three'));
> one
> two
> threee

It is just a draft to give a closer look to idea, different implementations will require different ways to call/implement it (i.e. partial functions, where you pass only some part of parameters in call and joining them with the ones left while doing a second call, like in Scala; btw, Function.bind() allows this, again), but I really doubt that proper variants of these implementations will take the same amount of KBs from user as most of those libs do — most of them are few-liners. Be monadic ;).

Achievements
138 Karma
5,404 Total ProTip Views