Skip to content

Instantly share code, notes, and snippets.

@joshnesbitt
Created November 29, 2011 15:54
Show Gist options
  • Select an option

  • Save joshnesbitt/1405289 to your computer and use it in GitHub Desktop.

Select an option

Save joshnesbitt/1405289 to your computer and use it in GitHub Desktop.
(function(window) {
// stuff that uses window a lot here...
})(window);
@joshnesbitt
Copy link
Copy Markdown
Author

In this instance, assume you are operating inside a closure, and that there is a lot of code in the anonymous function that accesses the window property.

If the window property was not passed into the local scope, every time window was accessed it would have to traverse up to the top level scope to find the actual window object. By passing it into the local scope we save any further lookups other than the current scope, reducing more intensive lookups to the top level scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment