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?
The value of
thisis determined upon every function call. BecauseBis called without any context, the value ofthisis the global object.It’s possible to preserve
thisin an outer context by simply copying it: