Skip to content

Instantly share code, notes, and snippets.

@kgarfinkel
Created October 20, 2013 00:53
Show Gist options
  • Save kgarfinkel/7063475 to your computer and use it in GitHub Desktop.
Save kgarfinkel/7063475 to your computer and use it in GitHub Desktop.
Underscore.js's 'once' re-implemented. Returns a function that can only be called one time. Repeated calls to the modified function will result in returning the value from the original call
_.once = function(func) {
var alreadyCalled = false,
result;
return function() {
if(!alreadyCalled) {
result = func.apply(this, arguments);
alreadyCalled = true;
}
return result;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment