0

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 Unexpected behavior of console.log

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

Slytherin
  • 474
  • 3
  • 17
  • is it possible the between the time of logging and the time when you expand the object in the console view, the object itself has been altered by your code already? – user1600124 Aug 26 '14 at 06:14
  • can you post some of your code or illustrate your area of concern via jsfiddle – Nick K Aug 26 '14 at 06:19
  • @user1600124 It could be , and I didn't think of this at all , it is actually always the same object logged. Is this a possibility that it could change after logging it? I always thought log is static thing – Slytherin Aug 26 '14 at 06:21
  • expanding the object in the console view would actually get an updated view of the object. So if the object is mutating, you end up with different results. If the object has no cyclic structure and contains no functions, perhaps you can log it as a json string, which would preserve the state no matter what you do – user1600124 Aug 26 '14 at 06:25
  • @user1600124 Hey thanks ! That did it , i didn't know at all this behavior of `console.log` is a thing. Please make an answer out of this so I can accept it – Slytherin Aug 26 '14 at 06:31
  • possible duplicate of [Is Chrome's JavaScript console lazy about evaluating arrays?](http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – slebetman Aug 26 '14 at 06:38

1 Answers1

0

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.

user1600124
  • 440
  • 2
  • 11