【问题标题】:global variable is not updated when changed value in another file nodejs当另一个文件nodejs中的值更改时,全局变量不会更新
【发布时间】:2021-12-30 06:05:27
【问题描述】:

在 nodejs 中,我尝试更新从另一个文件导出的全局变量并再次检查结果。原文件和导入文件中的变量值不同。

我运行文件 test.js 并检查结果。 DataCommon.js 和 test.js 中 gDataChange 的结果是不同的,虽然是同一个变量。

DataCommon.js 中,我导出了 gDataChange 变量

let gDataChange = [];

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

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

module.exports = {
    gDataChange,
    printArray,
    setArray
}

test.js中,我将一些数据push到全局数组中,并调用函数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);

如果我使用array.splice(),2个文件gDataChange是相同的。但是如果我使用 array.filter() 并像上面那样重新分配数组,则 2 文件的 gDataChange 是不同的。 当我用 array.filter() 重新分配时,我不确定 gDataChange 是否被创建到新的,因为在这种情况下我无法检查变量的地址。

【问题讨论】:

    标签: javascript node.js arrays module javascript-objects


    【解决方案1】:

    Node.js 模块系统是单例和模块缓存,模块系统一次又一次地引用完全相同的文件。例如:

    counter.js

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

    app.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
    

    在您的情况下,它的发生方式相同。但是,您正在更改数组 gDataChange 的引用。检查以下代码:

    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.

    因此,值的变化不是因为节点,而是因为您正在更改对对象的引用。这就是 JS 对象的工作方式。即使您分配过滤器数组值,您也在创建新数组。

    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
    }
    

    您可以查看node modules 的解析方式。

    【讨论】:

    • 谢谢。这对我的确认很有帮助。
    猜你喜欢
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 2016-01-17
    相关资源
    最近更新 更多