Created
October 20, 2013 00:53
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.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