Skip to content

Instantly share code, notes, and snippets.

@mattbaker
Created January 6, 2012 17:12
Show Gist options
  • Select an option

  • Save mattbaker/1571493 to your computer and use it in GitHub Desktop.

Select an option

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

srph commented Jul 27, 2015

Copy link
Copy Markdown

You mean x.get?

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