Skip to content

Instantly share code, notes, and snippets.

@seanmonstar
Created February 24, 2010 20:50
Show Gist options
  • Save seanmonstar/313836 to your computer and use it in GitHub Desktop.
Save seanmonstar/313836 to your computer and use it in GitHub Desktop.
Function.prototype.lazy = function() {
var fn = this,
init = false;
return function() {
if(!init) {
init = true;
fn = fn.apply(this, arguments);
}
return fn.apply(this, arguments);
}
};
var digit_name = function(n) {
var names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
return function(n) {
return names[n];
};
}.lazy();
digit_name(3); //init, then return
digit_name(2); //return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment