javascript - Global Variables alternative - Stack Overflow

I want to create an array that is accessible to all the .js files.This array must also be updateable,

I want to create an array that is accessible to all the .js files. This array must also be updateable, and the updated changes must be reflected in all the other files using that array.

I have read that using a GLOBAL variable is not a good method, but what is the alternative?

//file 1 which contains my global variable
var mySharedArray = ['helo']; 
module.exports = {
mySharedArray: mySharedArray,
get: function(){
    console.log(mySharedArray);
} 
}    

//file 2 where i am pushing new-data 
router.get('/workerequestest',function(req,res){
queue.push("moreData");
require('../config/globalQueue').get();
require('../config/startWorkerQueue');
res.send(200,'message recieved');
});

//file 3 where i am checking the updated values
var interval = setInterval(function() {
var Queue = require('../config/globalQueue').mySharedArray;
require('../config/globalQueue').get();
}, 10000,'hello' ,'checking for New processes');

I want to create an array that is accessible to all the .js files. This array must also be updateable, and the updated changes must be reflected in all the other files using that array.

I have read that using a GLOBAL variable is not a good method, but what is the alternative?

//file 1 which contains my global variable
var mySharedArray = ['helo']; 
module.exports = {
mySharedArray: mySharedArray,
get: function(){
    console.log(mySharedArray);
} 
}    

//file 2 where i am pushing new-data 
router.get('/workerequestest',function(req,res){
queue.push("moreData");
require('../config/globalQueue').get();
require('../config/startWorkerQueue');
res.send(200,'message recieved');
});

//file 3 where i am checking the updated values
var interval = setInterval(function() {
var Queue = require('../config/globalQueue').mySharedArray;
require('../config/globalQueue').get();
}, 10000,'hello' ,'checking for New processes');
Share Improve this question edited Jan 19, 2016 at 5:49 Furious asked Jan 18, 2016 at 19:31 FuriousFurious 4832 gold badges7 silver badges21 bronze badges 3
  • a module that gets required where said variable is needed. – Kevin B Commented Jan 18, 2016 at 19:34
  • This doesn't have anything to do with node.js. – Blubberguy22 Commented Jan 18, 2016 at 19:34
  • thanks for being quick. can you be little more specific.. may be explain with an example. – Furious Commented Jan 18, 2016 at 19:35
Add a ment  | 

2 Answers 2

Reset to default 9

Proper modularity in node.js does not use actual globals as that makes actually taking advantage of reusable modules more plicated and makes for possible conflicts with other globals.

One design pattern that can be used instead is to put the shared data in its own module and then just require() in that module wherever needed.

// shared-data.js
var mySharedArray = [...];

module.exports = {
    mySharedArray: mySharedArray
}

Then, in each module that wants to have access to that array, you can do this:

// someothermodule.js

var sharedArray = require('./shared-data.js').mySharedArray;


// this modification will change the main array and will be seen by
// all modules that are sharing it
sharedArray.push("moreData");

This works because when arrays are assigned in Javascript, they are not copied, but instead a pointer to the original array is shared. So, any modifications to the assigned value is just pointing at the original object and thus changes the original array. Thus, all modules would have access to the same original array.


I just tested this concept in my own three files and it works fine. Here are my three files:

Server File:

// shared-server.js
var express = require('express');
var app = express();
var server = app.listen(80);
var shared = require('./shared-var');
require('./shared-interval.js')

var cntr = 0;

app.use(express.static('public'));

app.get("/test", function(req, res) {
    shared.mySharedArray.push(++cntr);
    shared.get();
    res.send(200,'message received');
});

Shared Variable File:

//shared-var.js
var mySharedArray = ['helo']; 
module.exports = {
    mySharedArray: mySharedArray,
    get: function(){
        console.log(mySharedArray);
    } 
}

Interval File:

//shared-interval.js
var interval = setInterval(function() {
    require('./shared-var').get();
}, 1000);

When I run this, it outputs the value of mySharedArray every second. Each time I go to http://localhost/test with my browser, it adds a new value to mySharedArray and I see that in the console. This is the behavior I would expect.

You could put them in a separate module that gets required in whatever module you need it in, else you could use a seperate storage service such as redis or memcached to store your array of data.

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

相关推荐

  • javascript - Global Variables alternative - Stack Overflow

    I want to create an array that is accessible to all the .js files.This array must also be updateable,

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信