In Javascript what I know is that, scopes are handled using data structure called declarative environment record and the global environment has extra one called object environment record [1][2].
- object environment record handles var and function declaration
- declarative environment record handles let , const and class declarations
But, in Node.js it seems that this behaviour doesn't follow rules above. As
//in node.js
var a = 1;
console.log(global.a) //prints undefined so OER didn't handle this declaration
b = 2;
console.log(global.b) //prints 2 so declarations without var makes the OER handle the variable declaration
So, Does the DER object handle var and function declarations instead of the OER in Node.js ? and is it shared between modules while requiring them like the OER ?
Ex
//module foo.js
var x = 10;
y = 20;
and in another file
//module bar.js
var foo = require("./foo");
console.log(x) //undefined
console.log(y) //prints 20