How do I give multiple JavaScript objects across multiple files access to same private variable?

前端 未结 3 1259
野的像风
野的像风 2020-12-07 03:50

If I want to span my JavaScript project across multiple source files, but have each file have access to the same private variable, how would one do that?

For example

3条回答
  •  情歌与酒
    2020-12-07 04:14

    One way I was able to accomplish this was to create a JS file that contained the global object.

    // Define a global object to contain all environment and security variables
    
       var envGlobalObj = {
          appDatabase:      process.env.YCAPPDATABASEURL,
          sessionDatabase:  process.env.YCSESSIONDATABASEURL,
          secretPhrase:     process.env.YCSECRETPHRASE,
          appEmailAddress:  process.env.YCAPPEMAILADDRESS,
          appEmailPassword: process.env.YCAPPEMAILPASSWORD
       }
     module.exports = envGlobalObj

    Then in the files I wish to reference this object, I added a require statement.

     var envGlobalObj = require("./envGlobalObj.js");

    This allowed me to centralize the environment and secrect variables.

提交回复
热议问题