title: Trying to understand the new keyword
body: <p>I'm trying to understand exactly what the javascript new keyword means, why it is necessary and when I should use it.</p>
  
  <p>Consider the following examples:</p>
  
  <pre><code>      var x = new function(){
          var self=this;
          this.myFunction = function(){alert('foo ' + self.v)}
          this.v='x';
        };
  
        var y = function(){
          var self=this;
          this.myFunction = function(){alert('foo ' + self.v)}
          this.v='y';
          return this;
        }();
  
  
        var f=function(){
          var self=this;
          this.myFunction = function(){alert('foo ' + self.v)}
          this.v='z';
        }
  
        var z = new f();
  
        x.myFunction();
        y.myFunction();
        z.myFunction();
  </code></pre>
  
  <p>x,y and z are all objects. Potentially with public and private member variables.</p>
  
  <p>x and z where constructed with the use of the new keyword. As far as I can tell, the new keyword simply executes the function and returns the context. So I guess in the above example x and y are essentially singletons since any reference to the original function is lost?</p>
  
  <p>Apart from that are they all exactly equivalent? If not what are the differences and when would I want to use one approach or avoid another?</p>
  
  <p>Thanks for any explanations.</p>