Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tdantas
Forked from dshaw/gist:378192
Created January 10, 2012 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdantas/1588474 to your computer and use it in GitHub Desktop.
Save tdantas/1588474 to your computer and use it in GitHub Desktop.
(function(window,document,undefined){ ... })(this,this.document);
// everyone's new favorite closure pattern:
(function(window,document,undefined){ ... })(this,this.document);
// when minified:
(function(w,d,u){ ... })(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.
// additionally it protects against the case where someone does `undefined = true`
// 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.
// and a horribley awesome idea. duck punch to switch up the args passed in on all ready functions
(function(oReady){
$.fn.ready = function(fn){
return oReady.call(this, function(){ fn.call(this, $, window, document); });
};
})($.fn.ready);
// which enables
$(function($,window,document,undefined){ alert(document) })
// temp01 ftw. thx!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment