Possible Duplicate:
How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)?
I am trying to breakdown some of the parts of jQuery to better understand what’s going on behind the scenes. For the most part, I am pretty clear on a lot of it’s methods, but the first line of code looks like this:
(function( window, undefined ) {
and the library ends like this
})( window );
I understand that this is immediate function invocation, but what does this do in context to the jQuery library? I’m not sure what I am looking at.
Also, where else would this be useful to us?
It is passing the current window object into jQuery so it has a reference to the window as a local object.
It is also not passing in a second parameter so that undefined will truly be “undefined”. The reason to do this is that it would be possible to assign undefined a value, so by expecting a value as a second parameter, but not getting one, jQuery is assuring itself that it really has undefined.