Skip to content

Instantly share code, notes, and snippets.

@furf
Forked from paulirish/gist:315916
Created February 26, 2010 17:44
Show Gist options
  • Save furf/315942 to your computer and use it in GitHub Desktop.
Save furf/315942 to your computer and use it in GitHub Desktop.
// everyone's new favorite closure pattern:
(function(window,document,undefined){ ... })(this,this.document);
// minified:
(function(b,a,c){ ... })(this,this.document);
// which means all uses of window/document/undefined inside the closure
// will be single-lettered, so big gains in minification.
// it also will speed up scope chain traversal a tiny tiny little bit.
// tech details:
(function(){ })() // is a self executing anonymous function
// and when we
(function(myname){ }('paul') // we're just bypassing a var declaration at the top
// and then in this case, `this` is always the global object when in global scope so
// we can safely use it.
// everyone's new favorite jQuery closure pattern:
(function(window,document,$,undefined){ ... })(this,this.document,jQuery);
// minified:
(function(b,a,c){ ... })(this,this.document);
// Adding some other useful constants
(function (window, document, $, TRUE, FALSE, UNDEFINED) {
//...
// would there be performance hit to using TRUE/FALSE, especially within internal functions?
// if so, is it worth the (minor?) compression benefit?
})(this, this.document, this.jQuery, !0, !1);
// minified:
(function(e,a,f,b,d,c){})(this,this.document,this.jQuery,!0,!1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment