all these code snippets run in nodejs.
below are demo1.js, ps: run these code in nodejs rather than browser js engine
// demo1.js
var1 = 'var1'; // in nodejs or Chrome-V8 js engine, this line would create a property named var1 into globalThis/this/window obj
console.log("[globalThis.var1]:", globalThis.var1); // var1
globalThis.var2 = 'var2';
console.log('[var2]:', var2); // var2
var var3 = 'var3'; // in nodejs, this line would not create a property named var3 into globalThis
console.log("[globalThis.var3]:", globalThis.var3); // undefined
function baz() {
var var4 = 'var4'; // in nodejs, this line would not create a property named var4 into globalThis
console.log('[globalThis.var4]:', globalThis.var4);
}
baz(); // undefined
(function () {
var var5 = 'var5'; // in nodejs, this line would not create a property named var5 into globalThis
console.log('[globalThis.var5]:', globalThis.var5);
})();
I can understand every thing in demo1.js.
below are demo2.js, ps: run these code in nodejs rather than browser js engine
// demo2.js
(function() {
(0, eval)("var foo = 123;"); (0, eval)("var foo = 123;"); // indirect call eval,create a property named foo into globalThis, which means create a global scope variable foo.
(0, function() { console.log('[globalThis.foo]:', globalThis.foo); })();
})();
console.log('[globalThis.foo]:', globalThis.foo); // 123
console.log('[foo]:', foo); // 123
In demo2.js, I do konw the comma operator would change the eval execution scope to global scope, and also according to Direct and indirect eval – MDN page:
screenshot from MDN eval
My question is:
demo1.js the function baz code block var var4 = 'var4'
or the IIFE code block var var5 = 'var5'
, in nodejs these two line would NOT create a property into globalThis.
But demo2.js the IIFE code block, var foo = 123
, in nodejs this line create a property into globalThis.
In both demo1.js and demo2.js, since variables are declared using the var keyword(like var var4
and var var5
in demo1.js, var foo
in demo2.js), why is only the var declaration in demo2.js create a property into globalThis?