To share variable between module you can use function to get the value of variable between the main and the 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 the value was changed..