我在这里缺少什么?无法得到它为什么我的控制台表现不同取决于while循环是否存在?
function findPath(){
var openArr = [];
var closeArr = [];
var morefun = {};
var morefun1 = {};
var morefun2 = {};
morefun.f = 1;
morefun1.f = 2;
morefun2.f = 3;
openArr.push(morefun1);
openArr.push(morefun2);
openArr.push(morefun);
console.log(Array.isArray(openArr));
console.log(openArr);
console.log(openArr.length);
while (openArr.length){
var current = openArr.pop();
closeArr.push(current);
}
}
findPath();
我从console.log(openArr)
获取 [Object, Object,Object]
length: 0 // when while loop is there.
获得
[Object, Object,Object]
0:Object
1:Object
2:Object
length:3 // without while loop
它似乎只是Chrome的东西,因为我的Firefox控制台显示类似的结果 - 当我点击数组获取详细信息时,我得到长度:0,循环和长度:3 w / o。我是否因执行令而错过了smth?
答案 0 :(得分:2)
当您向控制台写入内容时,它会创建一个字符串,表示当时对象的状态。但是,它还存储对实际对象的引用以及您在控制台中显示的内容。
扩展该引用会显示它的实际状态,其中字符串在打印时显示其状态 。如果要查看每个console.log
的数组的完整状态,可以将其转换为JSON。
console.log(JSON.stringify(openArr, null, 2));
示例:
var arr = [{ n: 1 }, { n: 2 }, { n: 3 }];
console.log(arr); // [Object, Object, Object]
arr.pop();
// Expand the array and you'll only see 2 elements
var arr2 = arr.slice(0);
// To see the full state of the array, serialize it
console.log(JSON.stringify(arr2, null, 2)); // [ { "n": 2 }, { "n": 3 } ]
console.log(arr2); // [Object, Object]
arr.pop();
// Expand the array and you'll only see 1 element