Closure vs Rewrite
At The Rich Web Experience 2008, Douglas Crockford gave an example of a function which would take an integer and return the word for that number by retrieving the word from an array. He started by having the array created globally and the function would access it. This is bad because the array is global. He then created the array each time the function was called. This is also bad because you are creating the array repeatedly. Finally, he did it with a closure, creating the array in a self executing function on page load and returning an inner function which can access the array. It was similar to this, minus the alert and validation: Read more…
January 11th 2009
3 Comments