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

后端 未结 4 736
醉酒成梦
醉酒成梦 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: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
    });
    

提交回复
热议问题