javascript - Node.js - Module exports static variable - Stack Overflow

I'm attempting to export a module that should store a hashtable of given information so that anoth

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?

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?

Share Improve this question asked Feb 27, 2015 at 19:54 Zach KauffmanZach Kauffman 4961 gold badge5 silver badges17 bronze badges 2
  • Just store it in a variable var result = randomize.randomize(rows); – adeneo Commented Feb 27, 2015 at 19:57
  • all you show is an empty array named hashTable. what happens after that? – user2524973 Commented Feb 27, 2015 at 21:07
Add a ment  | 

1 Answer 1

Reset to default 4

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742320917a4421790.html

相关推荐

  • javascript - Node.js - Module exports static variable - Stack Overflow

    I'm attempting to export a module that should store a hashtable of given information so that anoth

    8小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信