我不理解为什么在我还没有输入这些数字之前,
console.log
显示 d1
包含 [100,200,300]
。然而,普通的 for loop
正确地显示了 d1
的内部。请问有人能解释一下 Google Chrome 中 console.log
的这种行为/bug吗?
https://jsfiddle.net/ZSvyt/
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