简体   繁体   English

为什么循环修改未在行中引用的其他变量(Javascript)?

[英]Why is for loop modifying an additional variable not referenced in the line (Javascript)?

I set two global variables: 我设置了两个全局变量:

var topList = {
    a: {},
    b: {},
    c: {},
    d: {},
    e: {}
}

var compare = {
    a: {},
    b: {},
    c: {},
    d: {},
    e: {}
}

I have a function which populates each of them, and then uses a for loop to swap out the a object within the compare variable. 我有一个函数填充它们中的每一个,然后使用for循环来换出compare变量中的a对象。 Then it calls a function to compare the new compare to topList , and returns the better of the two (thus setting topList as the better of the two: 然后它调用一个函数将新的comparetopList compare ,并返回两者中较好的一个(因此将topList设置为两者中较好的一个:

function optimize(data){

    var rawList = data.slice();

    var aList = $.grep(rawList, function(e) { return e.position == "A" });
    var bList = $.grep(rawList, function(e) { return e.position == "B" });
    var cList = $.grep(rawList, function(e) { return e.position == "C" });
    var dList = $.grep(rawList, function(e) { return e.position == "D" });
    var eList = $.grep(rawList, function(e) { return e.position == "E" });

    topList.a = aList[0];
    topList.b = bList[0];
    topList.c = cList[0];
    topList.d = dList[0];
    topList.e = eList[0];

    compare = topList;

    for (i = 0, len = aList.length; i < len; i++) {
        compare.a = aList[i];
        topList = best(topList, compare);
    }
}

For some reason, it seems that when the line compare.a = aList[i]; 出于某种原因,似乎当行compare.a = aList[i]; is executed, it's not only swapping out the a object in the compare variable, but also the a object in the topList variable. 被执行,它不仅换出a物体在compare变量,也是a在对象topList变量。 As a result I'm always sending two identical lists through my "best" function, which makes it useless. 结果,我总是通过我的“最佳”功能发送两个相同的列表,这使得它无用。

I'm new to this. 我是新手。 Any help would be greatly appreciated! 任何帮助将不胜感激!

In an attempt to explain simply, when you do: 为了简单解释,当你这样做时:

var x = {};

You take an empty object and assign it to x . 您获取一个空对象并将其分配给x

If you then do: 如果你这样做:

var y = x;

You are taking the same object and assigning it to y as well. 您正在使用相同的对象并将其分配给y

From then, if you do... 从那以后,如果你这样做......

y.foo = 'bar';

You will find that... 你会发现......

alert(x.foo); // bar (!)

This is called assignment by-reference, and it is what happens in JavaScript with objects (note that arrays are objects too, with predefined methods). 这称为by-reference赋值,它是带有对象的JavaScript中发生的事情(注意,数组也是对象,使用预定义的方法)。

The opposite is assignment by-value, where the value is copied to the new variable. 相反的是赋值by-value,其中值被复制到新变量。

So because you have this by-reference assignment, changes you make in one place will affect the other. 因此,由于您具有此引用分配,因此您在一个位置进行的更改将影响另一个。 You will need to use a copying function to get a new object, unrelated to the first, with the same value. 您将需要使用复制函数来获取与第一个无关的新对象,并使用相同的值。

Because compare is a reference to topList. 因为compare是对topList的引用。 You don't need to put 你不需要放

compare =topList;

Simply, this would work : 简单来说,这可行:

compare .a = aList[0];
compare .b = bList[0];
compare .c = cList[0];
compare .d = dList[0];
compare .e = eList[0];

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

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