Node.js global variables

前端 未结 6 1411
耶瑟儿~
耶瑟儿~ 2020-11-22 13:06

I asked here: Does Node.js require inheritance?

And I was told that I can set variables to the global scope by leaving out the variable.

This does not

6条回答
  •  渐次进展
    2020-11-22 13:26

    The other solutions that use the GLOBAL keyword are a nightmare to maintain/readability (+namespace pollution and bugs) when the project gets bigger. I've seen this mistake many times and had the hassle of fixing it.

    Use a JavaScript file and then use module exports.

    Example:

    File globals.js

    var Globals = {
        'domain':'www.MrGlobal.com';
    }
    
    module.exports = Globals;
    

    Then if you want to use these, use require.

    var globals = require('globals'); // << globals.js path
    globals.domain // << Domain.
    

提交回复
热议问题