1

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
Kirollos Nasr
  • 315
  • 1
  • 7
  • [I can't reproduce it](https://repl.it/repls/SourHeftyElements) `a` and `global.a` seem to be identical. – VLAZ Oct 02 '19 at 11:28
  • @VLAZ in javascript (that's run in browsers not node.js environment) the global variable is a property in the global object so using *a* is the same as *this.a* but my question is, this behaviour doesn't happen in node – Kirollos Nasr Oct 02 '19 at 11:34
  • I've linked you to something that runs Node.js and `var a = 1`, followed by `console.log(a, global.a)` prints `1 1`, so the `var` declaration *does* get attached to the global object. – VLAZ Oct 02 '19 at 11:35
  • But it did't for me in windows 8.1 https://www.hacksparrow.com/nodejs/global-variables-in-node-js.html – Kirollos Nasr Oct 02 '19 at 11:39
  • The your code might be executed outside the global scope. Also, that article does show that `var name = "Sparrow"` changes `global.name` - I'm not sure why you're linking it. – VLAZ Oct 02 '19 at 11:42
  • this link says that variable declaration using *var* does *not* attach it to the global variable **in node.js** --the binding object of OER, that's what I want to ask. – Kirollos Nasr Oct 02 '19 at 11:45
  • Where does it say that? The REPL dump shows `var name = 'Sparrow'; // undefined` and then `global.name // 'Sparrow'` so `var` declarations clearly *do* affect properties on the global object. The REPL runs in the global scope. – VLAZ Oct 02 '19 at 11:48
  • *"...So now you know, "declare the variable without the var keyword", "add the variable to the global object", and "add the variable to the GLOBAL object", all are the same thing..."* – Kirollos Nasr Oct 02 '19 at 11:53
  • "*Even more surprising was that a variable declared with or without the `var` keyword got attached to the global object*" why do you keep ignoring this? Even the article states it and it's **demonstrated** that it happens. Yet you claim the opposite. – VLAZ Oct 02 '19 at 11:55
  • I made a clarification for my question – Kirollos Nasr Oct 02 '19 at 12:07
  • 2
    OK, this is now in *module scope*, it's not in global scope. The rules for module scope are designed to *isolate behaviour*, otherwise loading `moduleA` which defines `var foo = 1` and `moduleB` that defines `var foo = 2` would result in probably erroneous behaviour even when you don't intend it and would definitely matter the order you load them in. Hence why you need to be *explicit* if you want global variables. – VLAZ Oct 02 '19 at 12:13
  • How node manages the *module scope* in terms of *declarative environments* like other scopes – Kirollos Nasr Oct 02 '19 at 12:20
  • https://stackoverflow.com/questions/30287977/es6-module-scope https://ringojs.org/documentation/module_and_global_scope/ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#Other_differences_between_modules_and_standard_scripts – VLAZ Oct 02 '19 at 12:30

0 Answers0