This post explains applications of uncurrying and currying this in JavaScript. It has been triggered by a tweet of Brendan Eich’s.
obj.foo(arg1, arg2)transform it into a function with the signature
foo(obj, arg1, arg2)To understand why that is useful, we first need to look at generic methods.
// Simplified version of the actual implementation: Array.prototype.forEach = function (callback) { for(var i=0; i<this.length; i++) { if (i in this) { callback(this[i], i); } } };this can be considered an implicit parameter of forEach(). forEach() works with any this parameter that can perform the following tasks.
function printArgs() { Array.prototype.forEach.call(arguments, function (elem, index) { console.log(index+". "+elem); }); }forEach.call() has one more argument than forEach(): Its first argument is the value of this. Interaction:
> printArgs("a", "b") 0. a 1. bThere are several methods in JavaScript that are generic in this manner, most of them are in Array.prototype.
var toUpperCase = String.prototype.toUpperCase.uncurryThis(); > [ "foo", "bar", "baz" ].map(toUpperCase) [ 'FOO', 'BAR', 'BAZ' ]
Use case: Turn a generic method into a function. Uncurrying this allows you to make a method prettier when it is applied generically. Example:
Array.forEach = Array.prototype.forEach.uncurryThis(); function printArgs() { Array.forEach(arguments, function (elem, index) { console.log(index+". "+elem); }); }There is a proposal for doing this for all Array methods in a future version of ECMAScript.
Function.prototype.uncurryThis = function () { var f = this; return function () { var a = arguments; return f.apply(a[0], [].slice.call(a, 1)); }; };
Function.prototype.uncurryThis = function () { return this.call.bind(this); };
Function.prototype.uncurryThis = function () { var f = this; return function () { return f.call.apply(f, arguments) }; };The above code is still in the vein of “borrowing the call() method”.
function(self, arg) { return self.foo + arg; }and currying this turns it into
function(arg) { return this.foo + arg; }
Use case: letting a method pass its this to a nested function. Instead of writing
var obj = { method: function (arg) { var self = this; // let nested function access `this` someFunction(..., function() { self.otherMethod(arg); }); }, otherMethod: function (arg) { ... } }you can write
var obj = { method: function (self, arg) { // additional argument `self` someFunction(..., function() { self.otherMethod(arg); }); }.curryThis(), // introduce an additional argument otherMethod: function (arg) { ... } }We have turned the implicit parameter this into the explicit parameter self. In other words: we have gone from a dynamic this to a lexical self. JavaScript would be simpler if this was always an explicit parameter.
The implementation. Implementing curryThis():
Function.prototype.curryThis = function () { var f = this; return function () { var a = Array.prototype.slice.call(arguments); a.unshift(this); return f.apply(null, a); }; };
function uncurryThis(f) { return function () { return f.call.apply(f, arguments) }; } function curryThis(f) { return function () { var a = Array.prototype.slice.call(arguments); a.unshift(this); return f.apply(null, a); }; }Note that you have just manually uncurried this for these functions.
How does one write a JavaScript meta-program that can operate reliably on other objects in its JavaScript context (in a browser – within the same frame), despite the loading of arbitrary code later into that same frame? The problem is that the later code may arbitrarily modify, override, or remove methods on the primordial built-in objects. To make the problem tractable, we assume that the meta program consists of modules that are first evaluated before their frame has become corrupted.