In what scope are module variables stored in node.js?

后端 未结 4 677
醉酒成梦
醉酒成梦 2020-11-22 02:34

When I do this in my node.js module:

var abc = \'123\';

Where does it go? And by this I mean: in the browser it goes in window.abc

相关标签:
4条回答
  • 2020-11-22 03:21

    All the other answers are 100% correct, but I thought I would add an expanded/definitive list of the scopes within a Node.js application in case anybody comes across this via Google while starting off learning Node.js or JavaScript:

    Global Scope

    Anything declared without the var keyword in any file will be accessible from anywhere running in the same instance of the Node server:

    // foo.js
    bar = 'baz';
    
    
    // qux.js
    console.log(bar); // prints 'baz'
    

    Note that this is widely considered to be a bad idea, because it makes your app strongly 'coupled'– meaning that you'd have to open foo.js to work out why bar = 'baz' in qux.js

    Module Scope

    Anything declared with the var keyword at the top level (not inside a function or object, or any other block) of a node.js file is in module scope, and will be accessible from anywhere within the same file, but will not exist anywhere else:

    // foo.js
    var bar = 'baz';
    console.log(bar); // prints 'baz'
    
    
    // qux.js
    console.log(bar); // prints 'undefined'
    

    Function Scope

    Anything declared using the var keyword within a function will only be accessible from within that function, and not from anywhere else:

    // foo.js
    function myFunction() {
      var bar = 'baz';
      console.log(bar); // prints 'baz'
    }
    
    function myOtherFunction() {
      console.log(bar); // prints 'undefined'
    }
    
    
    
    // qux.js
    console.log(bar); // prints 'undefined'
    

    JavaScript is function scoped. Unlike other (block scoped) languages, variables declared in a block within a function are accessible from anywhere else in that parent function. For example, this means that if you declare a new variable inside inside a loop, it's accessible outside of that loop as well, as long as you're still inside the parent function:

    function myFunction() {
      while (thing === true) {
      var bar = 'baz';
      thing = false;
      }
      console.log(bar); // prints 'baz'
    }
    

    Shadowing

    If you 'redeclare' an existing variable, e.g. use the var keyword with a variable name that has been used already, then the value associated with that variable name is overwritten within the scope of the new declaration:

    var bar = 'foo';
    console.log(bar) // prints 'foo'
    
    function myFunction() {
      var bar = 'baz';
      console.log(bar);
    }
    myFunction(); // prints 'baz'
    console.log(bar) // prints 'foo'
    
    0 讨论(0)
  • 2020-11-22 03:26

    Unlike the browser, where variables are by default assigned to the global space (i.e. window), in Node variables are scoped to the module (the file) unless you explicitly assign them to module.exports.

    In fact, when you run node myfile.js or require('somefile.js') the code in your file is wrapped as follow:

    (function (exports, require, module, __filename, __dirname) {
         // your code is here
    });
    
    0 讨论(0)
  • 2020-11-22 03:35

    Node has a module scope, so var abc = '123' in a module will create a variable which is scoped to (and therefore, reachable only for code in) that module.

    See also http://nodejs.org/api/globals.html#globals_global

    0 讨论(0)
  • 2020-11-22 03:35

    Pretty old question, and if anyone is curious about ECMA specs about this question, here is the link

    And there is no way for direct access for module variables (except for imported modules):

    Lexical Environments and Environment Record values are purely specification mechanisms and need not correspond to any specific artefact of an ECMAScript implementation. It is impossible for an ECMAScript program to directly access or manipulate such values.

    0 讨论(0)
提交回复
热议问题