0

在下面的代码中,ipClassRangeTop[3] = ipClassRangeBottom[3] + subNetCount - 1; 似乎 subNetCount 也被添加到 ipClassRangeBottom[3] 中。这不是我所期待的结果。我只希望 ipClassRangeTop[3] 具有该值。

有人可以让我知道我可能做错了什么。我对任何类型的编码都很陌生,因此非常感谢任何帮助。

var ipAddress = "172.16.1.1";
var subNetCount = 1 << 6;
var ipClassRangeTop = new Array();
var ipClassRangeBottom = new Array();
var classMaskDec = 65536;
var loopCount = 3; //classMaskDec/subNetCount;

//console.log(ipAddress, "ip address");
ipAddress = ipAddress.split(".");
ipClassRangeBottom = ipAddress;
ipClassRangeBottom[2] = 0;
ipClassRangeBottom[3] = 0;
ipClassRangeTop = ipClassRangeBottom;
//console.log(ipAddress, "ip address");

while (loopCount > 0 ) {
//console.log(i++);
console.log(ipClassRangeBottom, "1st bottom in loop");

ipClassRangeTop[3] = ipClassRangeBottom[3] + subNetCount - 1;
console.log(ipClassRangeBottom, "after top [3] assign");

if (ipClassRangeTop[3] > 255) {
    ipClassRangeTop[2] = ipClassRangeTop[2] + 1;
    ipClassRangeTop[3] = subNetCount - 1;
    console.log("IF RAN");
}

console.log(loopCount);
console.log(ipClassRangeTop);
console.log(ipClassRangeBottom);

ipClassRangeBottom[3] = ipClassRangeBottom[3] + subNetCount;
console.log(ipClassRangeBottom);
loopCount--;
}

console.log(ipAddress, "ip address");
console.log(ipClassRangeTop);
console.log(ipClassRangeBottom);

谢谢,马克

4

1 回答 1

4

将一个数组的引用从一个变量分配给另一个变量不会复制该数组(在 JavaScript 中)。那是:

var a = [1, 2, 3];
var b = a;
b[0] = "Hello World!";
alert(a[0]); // alerts "Hello World!"

如果要制作数组的真实副本:

var a = [1, 2, 3];
var b = a.slice(0);
b[0] = "Hello World!";
alert(a[0]); // alerts "1"
于 2013-06-24T02:03:57.777 回答