It doesn't work in Node when using var
because testContext
is a local of the current module. You should reference it directly: console.log(testContext);
.
When you don't type var
, what happens is that testContext
is now a global var in the entire Node process.
In Chrome (or any other browser - well, I'm unsure about oldIE...), it doesn't matter if you use var
or not in your example, testContext
will go to the global context, which is window
.
By the way, the "global context" is the default this
of function calls in JS.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…