Q:

Scope of this in javascript function

If you have the following code:
var global = this;
function A () {
    function B () {
        return this;
    }
    return B();
}
var C = new A();
C === global // true

Why does the this in function B refer to the global space and not the this of the object A?

A:

The value of this is determined upon every function call. Because B is called without any context, the value of this is the global object.
It's possible to preserve this in an outer context by simply copying it:
function A() {
  var x = this;
  function B() {
    return x;
  }
  return B();
}

