Javascript allows us to write functions that use their parameters to build and return another (often anonymous) function with specific behaviour. Currying is an example of this.
To illustrate, this approach can be used to elegantly sort an array of objects on an arbitrary property:
var sortOn = function(property) {
return function(a,b) {
return a[property].localeCompare(b[property]);
};
};
var myArray = [ {id:1, title:'Hello'}, {id:2, title:'Aloha'} ];
myArray.sort( sortOn('title') ); // Aloha, Hello
myArray.sort( sortOn('id') ); // Hello, Aloha
Generally speaking, is there a word for a Javascript function that, based on its parameters, returns another function?