【问题标题】:Why my copy Variable change the originale value variable In node js?为什么我的复制变量更改节点js中的原始值变量?
【发布时间】:2019-12-26 17:52:49
【问题描述】:

我不知道为什么当我更改作为另一个 var 副本的变量时,两者都被更改了?这对我来说没有任何意义吗?

你能解释一下为什么吗?这是我第一次在 node js 中遇到这种反应,我在使用 Pointers 时在 C 或 C++ 中知道这一点,但在 node js 中我不知道为什么!

function getByType(where) {
    var object = {
        topMarque: null,
        posModel: null,
    };
    var countPer = "titre";
    var copywhere = where;
    if (where.categorie == "voiture") {
        countPer = "marqueNom";
        copywhere.marqueId = {
            [Op.ne]: null
        }
    } else {
        console.log("------------------------------------------");
        console.log(where)
        console.log("------------------------------------------");
        copywhere.titre = (where.titre) ? where.titre : {
            [Op.ne]: null
        }
        console.log("******************************************");
        console.log(where)
        console.log("******************************************");
    }
    return db.stats.findAll({
        attributes: [countPer, [db.sequelize.fn('COUNT', db.sequelize.col(countPer)), 'total']],
        group: [countPer],
        limit: 5,
        where: copywhere,
        order: [
            [db.sequelize.fn('COUNT', db.sequelize.col(countPer)), 'DESC'],
        ],
    }).then(function(count) {
        object.topMarque = count;
        return db.stats.findAll({
            attributes: ['pos', [db.sequelize.fn('COUNT', db.sequelize.col('pos')), 'total']],
            group: ['pos'],
            where: where
        }).then(function(countPosCu) {
            object.posModel = countPosCu;
            return Promise.resolve(object);
        });
    }).catch(error => {
        return Promise.resolve(object);
    })
}

在第一个日志中我有这个:

------------------------------------------
{ categorie: 'moto' }
------------------------------------------

在第二个日志中我有这个:

******************************************
{ categorie: 'moto', titre: { [Symbol(ne)]: null } }
******************************************

【问题讨论】:

  • 因为您没有复制任何内容,所以您引用了相同的符号。与 C 或 C++ 中的方法相同:如果您有一个对象的引用,如果您更改该对象,任何具有相同引用的东西都会看到这些更改。如果你想要一个新对象,那么你需要克隆它(并注意深克隆与浅克隆,也与 C 或 C++ 相同)。
  • 具体来说,copywhere = where 不会复制对象。
  • 感谢您的回复,所以您告诉我,当我制作 var copywhere = where 时,它​​不是新对象或新变量,它与原来的 where 相同,非常感谢您的解释。
  • 与 C 或 C++ 指针相同——它只是引用相同的东西。
  • @DaveNewton 非常感谢你,我以前不知道,我会审查我的代码以避免引用,再次感谢你的解释。

标签: javascript node.js api express sequelize.js


【解决方案1】:

如果您在 javascript 中处理对象或数组,无论您是否将数组/对象复制到另一个变量中,它都会引用相同的引用,它将更改属于同一引用的所有变量的值

请查看以下示例:

// Declaring Array
var arr1 = [1,2,3,4,6];

//Trying to copy array into another variable
var arr2 = arr1;
console.log("========================");
//Display Array one
console.log(arr1);
//Adding more value in arra1
arr1.push(10);
arr1.push(11);
console.log("========================");
////Display Array Two
console.log(arr2);
console.log("========================");

O/P:

========================
[ 1, 2, 3, 4, 6 ]        
======================== 
[ 1, 2, 3, 4, 6, 10, 11 ]
======================== 

正如您在上面的示例中看到的,我们声明一个数组并将同一个数组复制到另一个数组中,然后我们将两个数组推入 arr1 并显示 arry2将显示引用 arr1

的数组值

【讨论】:

    猜你喜欢
    • 2015-12-30
    • 2021-11-03
    • 2013-11-25
    • 2015-01-07
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    相关资源
    最近更新 更多