Weird console.logbehavior in chrome

I have not been able to reproduce this issue with another script, so please run this script: http://jsfiddle.net/kz4k7/

As you can see on line 154-157:

for (myKey in v) {
    console.log ("v [" + myKey + "] =" + v [myKey]); 
}
console.log (v);

you expect the first three lines to be output the same as the last line. However, I am getting this result in chrome: enter image description here

If I remove solveTriangle (v) on line 158 console.log(v);

works fine. It also works if I print the cloned version of the v object to the console.

Note: It's not finished yet, I just want to know why chrome is doing this. I also don’t want to explain why you did this? and other questions unrelated to this error.

Edit:

This is also the behavior in Safari.

Screenshot of Firefox + firebug:

enter image description here

+1


source to share


2 answers


console.log

shows what the object contains when you open it (by clicking on the arrow), not when it was registered. To be honest, your code is quite verbose and a little messy with a lot of commented code, but it looks like you are actually modifying the object after registering it.

You can confirm this log behavior with the following simplified test: http://jsfiddle.net/e7Gvn/ .



var o = {
    a: 1,
    b: 2
};

console.log( JSON.stringify(o) ); // force string logging, which doesn't change

console.log(o); // open the object and you'll see that `c = 3`...
o.c = 3;        // ... even though it was only set here

      

+1


source


This is a fairly simple problem. Chrome stores references to the object. If you look at the object after the script finishes, you get this observable behavior because the object has changed and is not the same as when it was run console.log

.



If you set a breakpoint on the containing line console.log(v);

and check the value, the result is what you expected.

+2


source







All Articles