title: what does `setTimeout` bind `this` to?
body: <p>I am trying to create an example that demonstrates why the idiom <code>var that = this</code> is necessary (e.g. as described <a href="https://stackoverflow.com/q/4886632/274677">here</a>).</p>
  
  <p>So, I started with an example of erroneous code that would fail to properly bind <code>this</code>. However, the code I wrote gave me some unexpected results (in another direction):</p>
  
  <pre><code>message = "message in global scope";
  
  // the below erroneous code aims to demonstrate why we need
  // the "var that = this" idiom to capture the "this" lexical scope
  var createLoggingFunctionWrongWay = function () {
  
    return function () {
      console.log(this.message);
    };
  };
  
  // the below *does* print "message in global scope"
  // i.e. fails in the way I expected
  createLoggingFunctionWrongWay.call({message:"message"})();
  
  
  
  // I was expecting the below to also print "message in global scope" as
  // well, yet it prints "undefined"
  setTimeout(createLoggingFunctionWrongWay.call({
    message: "message"
  }), 1000);
  </code></pre>
  
  <p>When running under <code>nodejs</code> I get:</p>
  
  <pre><code>$ nodejs foo.js 
  message in global scope
  undefined
  </code></pre>
  
  <p>My question is why doesn't the second call (that uses <code>setTimeout</code>) also fail in the same way and interpret <code>this</code> to point to the <code>global</code> object in <code>Node.js</code> (where the <code>message</code> variable resides)?</p>
  
  <p><strong><em>update</em></strong>
  When I inserted a <code>console.log(this)</code> inside the anonymous function, on the first invocation I get the global context object (where <code>message</code> resides), whereas on the second invocation (via the <code>setTimeout</code>) I get the following object:</p>
  
  <pre><code>{ _idleTimeout: 1000,
    _idlePrev: null,
    _idleNext: null,
    _idleStart: 1446483586705,
    _onTimeout: [Function],
    _repeat: false
  }
  </code></pre>