title: Javascript [this] keyword binding with new
body: <p>when reading the book <a href="https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&amp;%20object%20prototypes/ch2.md#chapter-2-this-all-makes-sense-now" rel="nofollow">You Don't Know JS: this &amp; Object Prototypes</a></p>
  
  <p>I found this example about <code>this</code> binding:</p>
  
  <pre><code>function foo(something) {
    this.a = something;
  }
  
  var obj1 = {
   foo: foo
  };
  
  var obj2 = {};
  
  obj1.foo( 2 );
  console.log( obj1.a ); // 2
  
  obj1.foo.call( obj2, 3 );
  console.log( obj2.a ); // 3
  
  var bar = new obj1.foo( 4 );
  console.log( obj1.a ); // 2
  console.log( bar.a ); // 4
  </code></pre>
  
  <p>I don't understand why after the execution of new obj1.foo(4) <code>console.log(obj1.a)</code> prints <code>2</code> and not <code>4</code>. And if I comment the line <code>obj1.foo(2)</code> the result of the log above is <code>undefined</code>.</p>