2
我不明白爲什麼console.log
顯示d1
包含[100,200,300]
甚至在我甚至引入這些數字之前。經常for loop
正確顯示內部d1
雖然。有人可以在Google Chrome中解釋這種行爲/缺陷console.log
嗎? https://jsfiddle.net/ZSvyt/爲什麼console.log顯示不正確的對象值?
var container = {};
container["0"] = [10, 20, 30];
var d1 = container;
console.log('before console.log');
console.log(d1); // <--- IT DISPLAYS [100, 200, 300]. WHY?
// before for loop
console.log('before for loop');
for (var i = 0; i < d1["0"].length; i++) {
console.log(d1["0"][i]);
}
container["0"] = [100, 200, 300];
console.log('after console.log');
console.log(d1);
// after for loop
console.log('after for loop');
for (var i = 0; i < d1["0"].length; i++) {
console.log(d1["0"][i]);
}
輸出:
before console.log
Object {
0: Array[3]
}
0: Array[3]
0: 100
1: 200
2: 300
before for loop
10
20
30
after console.log
Object {
0: Array[3]
}
0: Array[3]
0: 100
1: 200
2: 300
after for loop
100
200
300
有時嵌套的_Objects_會發生這種情況,因爲控制檯的查找可能會在引用被更改後發生。如果它很重要,請使用_JSON_ –