In D3.js you see a lot of functions that take an argument and return a property or method invocation on that argument, or even just the argument itself. Calls that look like this:

.data( function (d) { return d; }) .attr( ' title ' , function (d) { return d.value; }) .text( function (d) { return d.toFixed(); })

Rubyists have the sweet Symbol#to_proc method for converting a symbol into a function that accesses that method/property; they could write the latter two as:

.attr( ' title ' ,& :value ) .text(& :toFixed )

We can make similar—and even more powerful—shortcuts in JavaScript so that our D3 code is easier to type, easier to read, and easier to maintain.

function ƒ (name){ var v,params=Array.prototype.slice.call( arguments , 1 ); return function (o){ return ( typeof (v=o[name])=== ' function ' ? v.apply(o,params) : v ); }; } function I (d){ return d }

Note that the name of the first function—ƒ—is a legal JavaScript identifier that is also easy-to-type on OS X ( option-f ) and unlikely to collide with other names.

With this the original code can be written far more simply: