3 ms·
I should add that console.log has its pitfalls - in particular, it will log objects by reference, so if you are doing some manipulation later in a block, the va
by Bahamut 13y ago
I should add that console.log has its pitfalls - in particular, it will log objects by reference, so if you are doing some manipulation later in a block, the value that may be displayed in the Chrome inspector may reflect a value that was manipulated later on in a block of code.
- kylebrown 13y agoconsole.log(JSON.parse(JSON.stringify(object))) is an easy way to log a deep copy, as long as it doesn't have circular properties (happens with some DOM elements).
- masklinn 13y agoThat's not an issue of console.log, that's an issue of webkit's console.log. Firebug does not exhibit this behavior, for instance.
- Bahamut 13y agoPerhaps - I think a lesson to be taken though is to be careful about trusting console.log. It threw me into a loop when I first encountered it.
- eof 13y agoAgreed, I was pulling hair out of my head--convinced for a moment there was no order in the universe until I realized that console.log could time travel. It appears to me that webkit's logging by reference is actually fuzzy; after some magical amount of time the logged object will be displayed "how it used to be"; but in some cases like obj = {foo: 'something bad mwahahhaa'}; console.log(obj); obj.foo = 'something good ;-)'; it will just go ahead and time travel to 'something good' and you will never see the 'something bad'. however, if you expand the the object in the console (side note, would be nice to have an option to display expanded by default) it doesn't ever time travel and obj = {foo: 'something bad mwahahhaa'}; console.log(obj); // some magic amount of time or lines of execution obj.foo = 'something good ;-)'; will also not time travel
- niyazpk 13y agoThis is not the case anymore. They fixed this issue sometime back.
- Bahamut 13y agoI've seen this exhibited as lately as Chrome 30.
- ufo 13y agoAnother tricky aspect about console.log is that in IE console.log will crash if you pass certain built-in types to it (I remember it happening with XML Document objects).