Have you ever taken a look under the hood at the jQuery 1.4 source code and noticed how it\'s encapsulated in the following way:
(function(
Undefined
By declaring undefined
as an argument but never passing a value to it ensures that it is always undefined, as it is simply a variable in the global scope that can be overwritten. This makes a === undefined
a safe alternative to typeof a == 'undefined'
, which saves a few characters. It also makes the code more minifier-friendly, as undefined
can be shortened to u
for example, saving a few more characters.
Window
Passing window
as an argument keeps a copy in the local scope, which affects performance: http://jsperf.com/short-scope. All accesses to window
will now have to travel one level less up the scope chain. As with undefined
, a local copy again allows for more aggressive minification.
Sidenote:
Though this may not have been the intention of the jQuery developers, passing in window
allows the library to be more easily integrated in server-side Javascript environments, for example node.js - where there is no global window
object. In such a situation, only one line needs to be changed to replace the window
object with another one. In the case of jQuery, a mock window
object can be created and passed in for the purpose of HTML scraping (a library such as jsdom can do this).