Chromium logs wrong javascript values

我的未来我决定 提交于 2019-12-12 02:02:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!