问题
While building a game where there is nested array grid to represent tiles , i was trying to determine "neighboring" tiles type , and ran into a coding mistake.
When i tried to debug , i wanted to console.log some objects , but my browser gave me weird output. When the object is written on one line , the values seem to be somewhat right ( except for that mistake of mine ) but when i wanted to display the rest of the object (click and roll-down) , it showed me entirely different values. Is this caused by me ?
For better illustration , here is a screenshot
The object is declared like this
var TileFactory = function(){
this.l={
x:null,
y:null
};
this.neighbours={
top:null,
topR:null,
topL:null,
r:null,
l:null,
bot:null,
botR:null,
botL:null
};
this.buffer;
};
And then , this is manipulated via method several times ( chain of conditions ) rapidly
回答1:
There are multiple answers out there with more detailed explanation and solutions. But when expanding an object in console view, the console output the latest state of the object. Perhaps doing so to reduce memory use in case those objects was never really viewed. So if the object has mutated after you logged it, you see a different result when you expand it.
If the object has no cyclic structure and has no functions, logging it as json string would solve the issue. If it does have cyclic structure and/or functions, perhaps you need to find some deep object cloning methods.
Personally I'd avoid using these kind of logging technique though. Whatever object is logged like this, it cannot be gc'ed. Definite should remove those logs before publishing code.
来源:https://stackoverflow.com/questions/25499182/chromium-logs-wrong-javascript-values