title: Javascript creating objects - multiple approaches, any differences?
body: <p>I have seen a few different ways to instantiate objects in javascript, wanted to know the benefits/drawbacks of the various approaches and why you would use one over the other. </p>
  
  <p><strong>Approach 1</strong></p>
  
  <pre><code>var obj = {
      prop: value,
      .
      .
      .
  }
  </code></pre>
  
  <p>Approach one is standard approach, nothing new :)</p>
  
  <p><strong>Approach 2</strong></p>
  
  <pre><code>var obj = new function() {
      var prop1 = value1;
      var fn1 = function() {
      };
      .
      .
      .
  
      this.prop2 = value2;
      .
      .
      .
  }();
  </code></pre>
  
  <p>The function approach, I wanted to compare this approach with approach 3. The function approach is primarily used for encapsulation (correct?)</p>
  
  <p><strong>Approach 3</strong></p>
  
  <pre><code>var obj = (function() {
      var prop1 = value1;
      var fn1 = function() {
      };
      .
      .
      .
  
      return {
          prop2: value2,
          .
          .
          .
      }
  })();
  </code></pre>
  
  <p>With this approach, I do not quite see the reasoning behind its usage. How does it differ from approach 2? Both can be used to encapsulate logic.</p>
  
  <p>Is it so we can pass in parameters, so we can deal with any potential conflicts?? E.g jquery's <code>$</code> syntax - but you can also do this with approach 2...</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <hr>
  
  <p>I am aware the approach 1 and 3 are similar (in that they both return objects) however approach 3 also creates a closure. Which approach 2 also does.</p>
  
  <p>That is the basis of my question really, both 2 and 3 create closures, but what is the difference between them.</p>