Skip to content

Instantly share code, notes, and snippets.

@ryasmi
Last active December 11, 2015 07:18
Show Gist options
  • Save ryasmi/4565135 to your computer and use it in GitHub Desktop.
Save ryasmi/4565135 to your computer and use it in GitHub Desktop.
This runs a recursive function and assigns the result of the recursions to a variable without leaking the function in global scope.
var foo = (function me(x) {
if (x < 10) {
return me(x + 1);
}
else {
return x;
}
}(1));
// In this case foo will be equal to 10.
// Calling me(1) will throw a reference error when called outside the function "me".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment