Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created August 25, 2012 10:21
Show Gist options
  • Select an option

  • Save hitode909/3463313 to your computer and use it in GitHub Desktop.

Select an option

Save hitode909/3463313 to your computer and use it in GitHub Desktop.
function once(callback) {
var results = {};
var key = callback.toString();
return function() {
if (!results.hasOwnProperty(key)) {
results[key] = callback();
}
return results[key];
}
}
var hello = once(function() {
print("building hello...");
return "Hello, " + "World!";
});
var bye = once(function() {
print("building bye...");
return "Good " + "Bye!";
});
print(hello());
print(bye());
print(hello());
print(bye());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment