Closure Pattern
2010-07-18 Leave a Comment
Here’s a common closure pattern that I’ve been seeing a lot:
(function (window, document, undefined) {
/***
- undefined, unlike null or window, is not a reserved word and can be redefined. By redefining undefined with an undefined value, we can be sure that it is what we expect it to be.
- The local definition of these variables allows for a minor performance improvement where the scope chain does not have to be traversed when looking up their values.
- These variables can also be obfuscated when compressed (e.g., because window is a local variable in this scope, all occurrences of it may be replaced by a single letter variable name).
***/
})(this, document);