When I define a local variable under global namespace, it gets overwritten by an instance variable of the same name.
var outerVar;
outerVar = 'outerVar';
this.outerVar = 'thisOuterVar';
console.log(outerVar); // thisOuterVar
However the same does not happen while inside a function.
var fun;
fun = function() {
var innerVar;
innerVar = 'innerVar';
this.innerVar = 'thisInnerVar';
return console.log(innerVar); // innerVar
};
fun();
This code was run in Firebug.
this.constructor // Window { }
If I run the same code under node, where this.constructor is [Function: Object], it returns 'outerVar' instead of 'thisOuterVar' which makes sense.
Why does it behave differently under Window?
new fun();and the result will be different (this.innerVarin your example without new is just the same aswindow.innerVar).