3

Possible Duplicate:
How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)?

I am trying to learn about js scopes and anonymous functions. I have tried to read the jQuery.js file and it looks like this:

(function( window, undefined ) {

...

}(window));

Why does it have in the function params undefined when no parameter is being passed to it when it is executed?

Community
  • 1
  • 1
Alon
  • 7,618
  • 18
  • 61
  • 99
  • 1
    It is already explained in here http://stackoverflow.com/questions/2716069/how-does-this-javascript-jquery-syntax-work-function-window-undefined – Andromeda Sep 10 '12 at 07:54
  • Read ScottLahteine's comment in response to the top answer to [this question](http://stackoverflow.com/questions/776950/javascript-undefined-undefined). It's to ensure that, within the method, the variable called `undefined` is genuinely undefined. – Damien_The_Unbeliever Sep 10 '12 at 07:56

1 Answers1

9

This method is used so you can be sure that no one has previously redefined undefined value with something like

var undefined = true; 

or with other tricky/evil assignments outside the jQuery scoped function. So, inside that function every comparison done against undefined is safe.

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177