Created
May 22, 2011 20:53
-
-
Save Tigraine/985884 to your computer and use it in GitHub Desktop.
Once function in JS
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 = (func) -> | |
calls = 0 | |
-> | |
calls+=1 | |
if (calls > 1) | |
throw "baad girl" | |
func.apply(this, arguments) | |
m = once -> alert "hello" | |
m() | |
m() |
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
var m, once; | |
once = function(func) { | |
var calls; | |
calls = 0; | |
return function() { | |
calls += 1; | |
if (calls > 1) { | |
throw "baad girl"; | |
} | |
return func.apply(this, arguments); | |
}; | |
}; | |
m = once(function() { | |
return alert("hello"); | |
}); | |
m(); | |
m(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment