Skip to content

Instantly share code, notes, and snippets.

@PatrickJS
Created July 24, 2013 14:54
Show Gist options
  • Select an option

  • Save PatrickJS/6071330 to your computer and use it in GitHub Desktop.

Select an option

Save PatrickJS/6071330 to your computer and use it in GitHub Desktop.
store your data in closure scope in order to optimise your functions
/*
// 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