title: What is the purpose of passing arguments to anonymous functions in this manner?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/111102/how-do-javascript-closures-work">How do JavaScript closures work?</a>  </p>
  </blockquote>
  
  
  
  <p>I was playing around with the <a href="http://closure-compiler.appspot.com/home" rel="noreferrer">Google Closure Compiler</a>, putting in random code to see what it would do.</p>
  
  <p>It rewrote one of my functions to look something like this:</p>
  
  <pre><code>(function(msg) { console.log(msg); })("Hello World!");​​​​​​​
  </code></pre>
  
  <p>Where it appears that <code>"Hello World"</code> is the argument passed as <code>msg</code> to the anonymous function preceding it. I was looking at it for a moment, and had thought that I had seen something similar in jQuery plugins that look something like:</p>
  
  <pre><code>(function( $ ) {
    ...
  })(jQuery); 
  </code></pre>
  
  <p>Which now makes more sense to me, in the scope of conflicts with <code>$</code>. But what is the primary reason or purpose for passing arguments into an anonymous function like this? Why wouldn't you simply define the arguments as variables within the function? Is there any performance or flexibility advantage to writing functions like this?</p>