Skip to content

Instantly share code, notes, and snippets.

@mattbaker
Created January 6, 2012 17:12
Show Gist options
  • Save mattbaker/1571493 to your computer and use it in GitHub Desktop.
Save mattbaker/1571493 to your computer and use it in GitHub Desktop.
Thunk in Javascript
function Thunk(fn) {
this.get = function() {
var v = fn();
this.get = function() { return v };
return v;
}
}
/*
* > var x = new Thunk(function() { console.log("working..."); return 2 * 2 * 2; })
* > x.getValue()
* working...
* 8
* > x.getValue()
* 8
*/
@srph
Copy link

srph commented Jul 27, 2015

You mean x.get?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment