title: How does JavaScript determine when to give a function call a "this" context?
body: <p>For obvious reasons, in JavaScript, the following two calls are different:</p>
  
  <pre><code>foo.bar();
  
  var bar = foo.bar;
  bar();
  </code></pre>
  
  <p>Namely, in the first call, <code>this</code> is the <code>foo</code> object. In the second, it's a reference to the global scope. However, the following example is a little less intuitive:</p>
  
  <pre><code>(foo.bar)();
  </code></pre>
  
  <p>I would expect it to operate the same way as the second example, but it actually operates the same as the first. That is, <code>this</code> references <code>foo</code>, not the global scope.</p>
  
  <p>What are JavaScript's rules for deciding when to make a function call a "method call" and when to simply call the function without a particular <code>this</code>?</p>
  
  <h3>EDIT:</h3>
  
  <p>As <a href="https://stackoverflow.com/users/218196/felix-kling">Felix Kling</a> points out in a comment, I'm wondering why the third example doesn't use the <code>window</code> context when theoretically it should simply retrieve the function and call it without the additional context. His example clarifies my question a little:</p>
  
  <pre><code>(true &amp;&amp; foo.bar)(); // 'this' refers to the global scope
  </code></pre>