Friday, November 22, 2013: The "itself" Recipe

I’m tired of having to write function(x) { return x.something } again and again and again and again and again, and I’m working on a very small standalone script, so using it.js seems to be an overkill for me.

So I came up with a simple recipe:

function itself(code) {
  return new Function('its', 'return its' + code)
}

var its = itself

Let’s see some use cases…

;['hello\n', 'world\n', 'test\n'].map(itself('.trim()'))
// => [ 'hello', 'world', 'test' ]

An npm module is available!

npm install itself

That version also has function caching, so it should be extremely fast.