admin管理员组文章数量:1203220
I have reason to believe console.log
and JSON.stringify
can produce inconsistent views of the same object even if it was created in a straightforward manner (see notes).
Situation
In both Google Chrome developer tools and Firebug, I had an object obj
which console.log
printed out as { players: {0: ...}, ...}
, while JSON.stringify
reported { players: {}, ...}
. obj.players
was {}
under both functions, so it seems that console.log
is the culprit. Could it be asynchronous/non-deterministic in some way?
Additional notes
I'm afraid I won't be able to provide much more context, since the code is lengthy and for a client, but I can try if there is something that could help get to the bottom this. For the moment, I am forced to stay away from console.log
for inspection.
It might be useful to know that the object is formed merely from an object literal by setting properties by hand, e.g., obj.players = {}; obj.players[0] = ...
.
Code
A sample of what I mean can be observed at /.
I have reason to believe console.log
and JSON.stringify
can produce inconsistent views of the same object even if it was created in a straightforward manner (see notes).
Situation
In both Google Chrome developer tools and Firebug, I had an object obj
which console.log
printed out as { players: {0: ...}, ...}
, while JSON.stringify
reported { players: {}, ...}
. obj.players
was {}
under both functions, so it seems that console.log
is the culprit. Could it be asynchronous/non-deterministic in some way?
Additional notes
I'm afraid I won't be able to provide much more context, since the code is lengthy and for a client, but I can try if there is something that could help get to the bottom this. For the moment, I am forced to stay away from console.log
for inspection.
It might be useful to know that the object is formed merely from an object literal by setting properties by hand, e.g., obj.players = {}; obj.players[0] = ...
.
Code
A sample of what I mean can be observed at http://jsfiddle.net/9dcJP/.
Share Improve this question edited Jan 4, 2012 at 20:58 Peteris asked Jan 4, 2012 at 20:50 PeterisPeteris 3,7564 gold badges31 silver badges46 bronze badges 7 | Show 2 more comments2 Answers
Reset to default 17Why don't you just use console.dir(obj)
instead? Or use console.log(obj)
and then click on the output / expand it?
Also when I try the following code:
var obj = {};
obj.players = {};
obj.players[0] = {color: "green"};
obj.players[1] = {color: "blue"};
obj.world = "xyz";
console.log(JSON.stringify(obj));
I always (Firefox, Chrome, Opera) get this as output:
{"players":{"0":{"color":"green"},"1":{"color":"blue"}},"world":"xyz"}
Furthermore it looks like your players
object is actually an array. So you should better create it as such.
Update:
I just saw that you added a link to a JSFIDDLE demo, which empties the players
object right after logging it. To my knowledge all web dev tools use some kind of asynchronous display, which results in an empty players
object when using console.log(obj)
or console.dir(obj)
. Firebug thereby offers the best results by displaying the object correctly using console.dir(obj)
.
To answer the question - yes, console.log operations are asynchronous so can't be relied upon to print accurate values in the console - especially if you modify the printed object straight after the console.log call (like you are doing).
If you take away the line:
obj.players = {};
then both the differences between the plain console.log and the JSON.stringify dissappear.
Bearing in mind that there is a difference between logging a real object in a developer tools console (console.log(obj) and printing the stringified version of the object (console.log(JSON.stringify(obj))). So the representation of the object will still be different.
本文标签: javascriptconsolelog inconsistent with JSONstringifyStack Overflow
版权声明:本文标题:javascript - console.log inconsistent with JSON.stringify - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738574981a2100822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
console.log(JSON.stringify(obj));
? – Matt H Commented Jan 4, 2012 at 20:51console.log(JSON.stringify(obj))
was what I meant by usingJSON.stringify
. – Peteris Commented Jan 4, 2012 at 20:54console.log
does have some asynch issues in Chrome if you pass it an object and then modify that object immediately. You can work around it by reducing what you pass toconsole.log
to a string first since strings are immutable, nothing gets changed before it's output. – jfriend00 Commented Jan 4, 2012 at 21:09