Created
July 24, 2013 14:54
-
-
Save PatrickJS/6071330 to your computer and use it in GitHub Desktop.
store your data in closure scope in order to optimise your functions
This file contains hidden or 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
| /* | |
| // Slow way without Closure | |
| var ditgitName = function(n) { | |
| var names = ['zero', 'one', 'two', | |
| 'three', 'four', 'five', | |
| 'six', 'seven', 'eight', 'nine']; | |
| return names[n] | |
| } | |
| */ | |
| // Closure way | |
| var digitName = (function() { | |
| var names = ['zero', 'one', 'two', | |
| 'three', 'four', 'five', | |
| 'six', 'seven', 'eight', 'nine']; | |
| return function(n) { | |
| return names[n]; | |
| } | |
| }()); | |
| alert(digitName(3)); //=> 'three' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment