console.log inconsistent with JSON.stringify

前端 未结 2 1532
孤独总比滥情好
孤独总比滥情好 2021-02-20 18:11

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 m

2条回答
  •  囚心锁ツ
    2021-02-20 18:55

    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.

提交回复
热议问题