Monday 12 June 2017

module.export.variable is always an empty object if its reinitialized

I am confused a bit about the module.exports functionality in the below examples. In case 1, the a.arrayVar in b.js correctly gives all the elements that get added to the array in a.js even during the run time, but the same does not happen in case 2. The only difference in both the cases is the arrayVar is reinitialized in the function in case 2.I have a use case where the array must be reinitialized everytime it is updated dynamically and I am finding it difficult to implement this case. Any help in me understanding the concept in greatly appreciated.

case 1 :

// a.js
var arrayVar= [];
module.exports.arrayVar= arrayVar;

function test(element){
arrayVar.push(element);
}


// b.js
var a= require('./a.js');
console.log(a.arrayVar);

case 2 :

// a.js
var arrayVar= [];
module.exports.arrayVar= arrayVar;

function test(element){
 arrayVar= [];
 arrayVar.push(element);
}

// b.js
var a= require('./a.js');
console.log(a.arrayVar);



via KBSri

No comments:

Post a Comment