-
-
Save kangax/671368 to your computer and use it in GitHub Desktop.
// Chrome pet peeve with console logging | |
var arr = [ ]; | |
for (var i = 0; i < 3; i++) { | |
arr.push(i); | |
console.log('foo', arr); | |
} | |
/* | |
Actual: | |
(3) foo [0, 1, 2] | |
Expected: | |
foo [0] | |
foo [0, 1] | |
foo [0, 1, 2] | |
*/ |
Not exactly. It's basically saying all 3 are the exact same (which is inaccurate) and therefore it's just bumping up the count to the left of it. Either way it ain't right.
It looks like it's saving the console log calls up until the end and then executing from some sort of queue. If it's executing after the for statement is completed (or at the end of the entire script) arr would be [0,1,2] for each of the calls, so saying it's 3 of the same would be accurate.
Aaahh, I see it, you are right. Throwing a simple breakpoint in it makes it work as expected.
Yeah, this is an even more unexpected result:
https://gist.github.com/671630
From what I understand, collapsing occurs when statements are on the same line.
console.log(1); console.log(1) // shows "(2) 1"
// but
console.log(1);
console.log(1); // shows "1" followed by "1"
Ditto for when statements are in the loop (as they correspond to the same line number).
I don't know, man, still a little odd. Checkout that gist above. Also, try this (all one line):
var i=1,arr=[];arr.push(i);console.log(arr);arr.push(i);console.log(arr);
So it waits until the script is done to write out the logs instead of writing as they occur?