简体   繁体   English

当另一个文件nodejs中的值更改时,全局变量不会更新

[英]global variable is not updated when changed value in another file nodejs

In nodejs, I try to update the global variable exported from another file and check the result again.在 nodejs 中,我尝试更新从另一个文件导出的全局变量并再次检查结果。 The values of the variable in original file and imported file are different.原始文件和导入文件中的变量值不同。

I run file test.js and check the result.我运行文件 test.js并检查结果。 The result of gDataChange in DataCommon.js and test.js is different although it is the same variable. DataCommon.js 和 test.js 中的 gDataChange 的结果是不同的,虽然它是同一个变量。

In DataCommon.js , I exported gDataChange variableDataCommon.js中,我导出了 gDataChange 变量

let gDataChange = [];

function printArray()
{
    console.log('DataCommon.js call ', gDataChange);
}

function setArray(lArray)
{
    gDataChange = [...lArray];
}

module.exports = {
    gDataChange,
    printArray,
    setArray
}

In test.js , I push some data into the global array and call function setArray to change it.test.js中,我将一些数据推送到全局数组中并调用 function setArray 来更改它。

var { gDataChange, printArray, setArray} = require('../../res/DataCommon.js');

if (!gDataChange.length)
{
    gDataChange.push(1);
    gDataChange.push(2);
    gDataChange.push(1);
    gDataChange.push(3);
}

function testGlobalVar() {
    let newData = [...gDataChange];
    newData = newData.filter((number)=>{
        return number != 1;
    });
    setArray(newData);
}

testGlobalVar();

console.log('test.js call ', gDataChange);
printArray();

setTimeout(() => {
    console.log(gDataChange);
}, 10000);

在此处输入图像描述

If I used array.splice() , the gDataChange of 2 file are the same .如果我使用array.splice() ,则 2 个文件的gDataChange是相同的 But if I used array.filter() and re-assigned array like above, the gDataChange of 2 file are the different.但是如果我使用array.filter()并像上面那样重新分配数组,则 2 文件的 gDataChange 是不同的。 I am not sure that gDataChange is created to the new one when I re-assigned with array.filter() because I cannot check the address of a variable in this case.当我用array.filter()重新分配时,我不确定 gDataChange 是否被创建到新的,因为在这种情况下我无法检查变量的地址。

The Node.js module system is Singleton and module caching, the module system refers to the exact same file again and again. Node.js模块系统是Singleton和模块缓存,模块系统一次又一次引用完全相同的文件。 For example:例如:

counter.js计数器.js

let value = 0

module.exports = {
  increment: () => value++,
  get: () => value,
}

app.js应用程序.js

const counter1 = require(‘./counter.js’)
const counter2 = require(‘./counter.js’)

counter1.increment()
counter1.increment()
counter2.increment()

console.log(counter1.get()) // prints 3
console.log(counter2.get()) // also prints 3

In your case, it is happening the same way.在您的情况下,它的发生方式相同。 However, you are changing the reference of your array gDataChange .但是,您正在更改数组gDataChange的引用。 Chek the below code:检查以下代码:

 let arr = [1, 2, 3, 4]; const arr1 = arr.filter(x => x % 2 === 0); // this filter returns new array that refers to the new filtered data. console.log(arr1); // new array const arrOld = arr; // assigned reference of old array to new variable arr = arr1; // assigning filtered data reference to arr console.log(arr, arrOld); // you are seeing this in your code.

So, the change in values is not because of node it is because you are changing the reference to objects.因此,值的变化不是因为节点,而是因为您正在更改对对象的引用。 This is how JS objects works.这就是 JS 对象的工作方式。 Even when you assign the filter array values, you are creating new array.即使您分配过滤器数组值,您也在创建新数组。

let gDataChange = [];

function printArray()
{
    console.log('DataCommon.js call ', gDataChange);
}

function setArray(lArray)
{
    gDataChange = [...lArray]; // here a new array reference with filter values are assigned.
// now if you change anything in the filter array it won't reflect here.
}

module.exports = {
    gDataChange,
    printArray,
    setArray
}

You can check how node modules are resolved.您可以检查节点模块的解析方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM