title: What advantages does using (function(window, document, undefined) { ... })(window, document) confer?
body: <p>I guess using this pattern is the new hotness, but I don't understand what the advantage is and I don't understand the scoping implications.</p>
  
  <p>The pattern:  </p>
  
  <pre><code>(function(window, document, undefined){
    window.MyObject = {
      methodA: function() { ... },
      methodB: function() { ... }
    };
  })(window, document)
  </code></pre>
  
  <p>So I have several questions about this.</p>
  
  <p>Is there a particular advantage to encapsulating an object like this?<br>
  Why are <em>window</em> and <em>document</em> being fed in instead of just being accessed normally?<br>
  Why the heck is <code>undefined</code> being passed in?<br>
  Is attaching the object we're creating directly to window a particularly good idea?  </p>
  
  <p>I'm used to what I'll call the Crockford style of Javascript encapsulation (because I got it off the Douglas Crockford Javascript videos).</p>
  
  <pre><code>NameSpace.MyObject = function() {
    // Private methods
    // These methods are available in the closure
    // but are not exposed outside the object we'll be returning.
    var methodA = function() { ... };
  
    // Public methods
    // We return an object that uses our private functions,
    // but only exposes the interface we want to be available.
    return {
  
      methodB: function() {
        var a = methodA();
      },
      methodC: function() { ... }
  
    }
  // Note that we're executing the function here.
  }();
  </code></pre>
  
  <p>Is one of these patterns functionally better than the other? Is the first one an evolution of the other?</p>