3

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

(function(window, undefined){})(window);

What is the meaning of this code? I have seen it in many documents, especially in jQuery documents.

How does this work, and why is it defined thus?

Community
  • 1
  • 1
teacher
  • 965
  • 3
  • 14
  • 27
  • 2
    http://stackoverflow.com/questions/2716069/how-does-this-javascript-jquery-syntax-work-function-window-undefined Will give you the answer to this question B-) – Tats_innit Jun 01 '12 at 05:37
  • 1
    You might check out this other question: http://stackoverflow.com/questions/2716069/how-does-this-javascript-jquery-syntax-work-function-window-undefined – francisco.preller Jun 01 '12 at 05:38

1 Answers1

2

You are scoping a piece of code..

By.

  1. Defining it within an anonymous function //function(){...}
  2. Executing it. //(function{})(args)

Also, passing the window parameter allows for faster resolution of the meaning of that variable within your block of code.

Robin Maben
  • 19,662
  • 16
  • 61
  • 93
  • Also known as a self-invoking function. It's used to prevent pollution of the global scope. – xbonez Jun 01 '12 at 05:46