title: Immediately-Invoked Function Expression (IIFE) vs not
body: <p>I see a lot of code like:</p>
  
  <pre><code>var myApp ={};
  (function() {
      console.log("Hello");
      this.var1 = "mark";     //"this" is global, because it runs immediately on load.  Caller is global
      myApp.sayGoodbye = function() {
          console.log("Goodbye");
      };
  })();
  </code></pre>
  
  <p>Which causes the anonymous function to execute immediately.  But what is the advantage of this, compared to just putting the code inline?</p>
  
  <pre><code>var myApp ={};
  console.log("Hello");
  var1 = "mark";     
  myApp.sayGoodbye = function() {
      console.log("Goodbye");
  };
  </code></pre>
  
  <p>Apparently it's to do with scope of the function, but as the function is anonymous and called by window, it's scope (i.e. <code>this</code>) is global, no?</p>