在我的代码中,行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);
谢谢, 标记
答案 0 :(得分: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"