Closure Pattern

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);

Advertisement

About Eugene Kashida
I tell browsers what to do.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.