Reputation: 496
I'm attempting to export a module that should store a hashtable of given information so that another call to access that information can be checked for existence in the hashtable, and if found, return the value in the hashtable.
I am having trouble getting the hashtable in the export to remain consistent throughout the app as a singleton/static/global variable.
Here's what I have:
var Randomize = {
hashTable: [],
randomize: function(rows) {
var randomized = [];
for(var i in rows) {
//check if exists in hashtable, use values accordingly
}
return randomized;
}
};
module.exports = Randomize;
And when I try to access it with:
var randomize = require('randomize');
/* ... */
console.log(randomize.randomize(rows))
It creates a new hashtable for each instance. How can I make it so that it reuses the same instance of hashtable?
Upvotes: 8
Views: 9849
Reputation: 8727
Your hashtable might be in the wrong scope - it's possibly being clobbered with each require
. Try this instead:
var hashTable = [];
var Randomize = {
hashTable: hashTable,
randomize: function(rows) {
var randomized = [];
for(var i in rows) {
//check if exists in hashtable, use values accordingly
}
return randomized;
}
};
module.exports = Randomize;
Upvotes: 4