Uncurrying 'this' in JavaScript
Join the DZone community and get the full member experience.
Join For FreeThis post explains applications of uncurrying and currying this in JavaScript. It has been triggered by a tweet of Brendan Eich’s.
To understand what uncurrying this is and why it is useful, we first need to look at generic methods.
Generic methods
Normally, a method is very closely tied to its type: You can only use it on an object if the object is an instance of the method’s type. However, some methods are so useful that it would be nice if we could apply them to instances of other types, too. For example:// 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.
- length property: this.length
- Property access: this[i]
- Checking for the existence of a property: i in this
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.
Uncurrying this
Uncurrying this in a method means going from a signatureobj.method(arg1, arg2)to a signature
method(obj, arg1, arg2)Uncurrying 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. The following are three ways of implementing uncurryThis.
Version 1: What is really happening [by Eich, slightly modified]?
Function.prototype.uncurryThis = function () { var f = this; return function () { var a = arguments; return f.apply(a[0], [].slice.call(a, 1)); }; };Version 2: The uncurried version of a function is the same as invoking the call() method on the original. We can borrow that method via bind():
Function.prototype.uncurryThis = function () { return this.call.bind(this); };Version 3: It is best to define standard methods without depending too much on external methods. Furthermore, bind() does not exist prior to ECMAScript 5. We thus rewrite version 2 as follows.
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”.
Currying this
The inverse of uncurryThis(), which is called curryThis(), is useful whenever a method wants to pass its this to a nested function. Instead of writingvar 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.
Implementing curryThis():
Function.prototype.curryThis = function () { var f = this; return function () { var a = Array.prototype.slice(arguments); a.unshift(this); return f.apply(null, a); }; };
JavaScript
Opinions expressed by DZone contributors are their own.
Comments