To exchange a variable between a module, you can use the function to get the value of a variable between the main and modules.
//myModule.js var mainFunction = null; //You can also put function reference in a Object or Array function functionProxy(func){ mainFunction = func; //Save the function reference } // --- Usage --- // setTimeout(function(){ // console.log(mainFunction('myString')); // console.log(mainFunction('myNumber')); // }, 3000); module.exports = { functionProxy:functionProxy }
.
//main.js var myModule = require('./myModule.js'); var myString = "heyy"; var myNumber = 12345; function internalVariable(select){ if(select=='myString') return myString; else if(select=='myNumber') return myNumber; else return null; } myModule.functionProxy(internalVariable); // --- If you like to be able to set the variable too --- // function internalVariable(select, set){ // if(select=='myString'){ // if(set!=undefined) myString = set; // else return myString; // } // else if(select=='myNumber'){ // if(set!=undefined) myNumber = set; // else return myNumber; // } // else return null; // }
You can always get the value from main.js even when changing the value.
StefansArya Jul 02 '17 at 9:24 on 2017-07-02 09:24
source share