Humor, Weltschmerz und jede Menge Meinungen eines Leipziger Studenten

Hauptmenü

Beitrags-Navigation

Using ExtJs‘ bind to partially apply a function

Because of my work I dabbled a bit in ExtJs and I wanted to show you something that I need quite regularly. It has become common practice in JavaScript to pass around function object like no man’s business. Which is quite alright. But sometimes you prepare a function for another library. Usually this looks something like this:

But you can’t pass any argument to somefunction (because JavaScript is not Haskell and you cannot build partially applied thunks). So how do we partially apply some values? ExtJs gives us quite a powerful tool:

Usually, you would bind the scope to „this“ but in this rather trivial case, we don’t need it. The „true“ at the end is to tell Ext to append the new parameters (3, 4 and 5) rather than overwriting the existing ones. But we can put them any place we want.

caller(Ext.bind(somefunction, undefined, [3,4,5], 1));
// 1 3 4 5 2

We also can, which is rather cool, apply them in instead and therefore „cancel out“ other paramters by leaving the last bind-parameter as undefined: