the statement:
for( var item in this){
console.log( item );
}
Is a noop in the global context. However in a function
function foo(){
for( var item in this){
console.log( item );
}
}
foo();
This produces the global environment objects.
What is the reason for this behaviour?
What is the syntax for accessing the objects currently in scope, as in the first sample?
The
thisis probably pointing to the exports object of node.So in the first case, the
thisis right not to point at thewindowobject. In the second case, well, thethisis inside a function (which is not a method of an object) so, as you’d expect, it points back to thewindow.