JavaScript Partials June 11th, 2014

Gratuitously copied from John Resig’s blog

Partially applying a function is a technique in which you can pre-fill-in arguments to a function before it is ever executed. In effect, partially applying a function returns a new function which you can call…

String.prototype.csv = String.prototype.split.partial(/,\s*/);
var results = "John, Resig, Boston".csv();
alert( (results[1] == "Resig") + " The text values were split properly" );

In the above case we’ve taken a common function – a String’s .split() method – and have pre-filled-in the regular expression upon which to split. The result is a new function, .csv() that we can call at any point to convert a list of comma-separated values into an array.

Resources