0

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?

Community
  • 1
  • 1
Sethen
  • 11,140
  • 6
  • 34
  • 65
  • Basically it's a way of avoiding polluting the global scope: http://stackoverflow.com/questions/3265823/javascript-global-scope – RichardTowers Dec 13 '12 at 17:10

1 Answers1

0

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.

Andy T
  • 10,223
  • 5
  • 53
  • 95