Re:Deep Cloning Objects in JavaScript, the Modern Way
by dylan604 on Thursday December 21, 2023 @07:19PM
(#38729132)
Isn't that desired? You want to see what the object looks at at the time of logging. If you need to see it after some additional code has run, you log it again.
Re:Deep Cloning Objects in JavaScript, the Modern Way
by Akronymus on Thursday December 21, 2023 @07:23PM
(#38729169)
The problem is that if you log one object multiple times, they all point to the same object.
So if between two logs it was modified, it reflects the new status on the log before as well.
I think you may have been confused and got how it is, vs how it ought to be the wrong way round.
Re:Deep Cloning Objects in JavaScript, the Modern Way
by jv22222 on Thursday December 21, 2023 @08:25PM
(#38729741)
That's just one of the reasons I always say...
https://twitter.com/justinvincent/status/1714866433426067573
Re:Deep Cloning Objects in JavaScript, the Modern Way
by dylan604 on Thursday December 21, 2023 @08:03PM
(#38729548)
to be honest, my experience with logging objects is so simple, i've never needed to try this live look into the object. my expectation was the log is a snap shot in time, not a live look at the object. an interactive view into the object does sound interesting rather than constant log entries for each step. which is how I would have tried to do something the first time only to be confused by it. TIL.
Re:Deep Cloning Objects in JavaScript, the Modern Way
by jholman on Thursday December 21, 2023 @10:19PM
(#38730584)
What the parent posts are trying to hint to you is that the default logging behaviour of the console is the live-look model, which is almost never what you wanted with logging. (Though it does have other uses.) To avoid the live-look, you can deep clone, or just stringify.
Re:Deep Cloning Objects in JavaScript, the Modern Way
by jay_kyburz on Thursday December 21, 2023 @08:11PM
(#38729619)
Wow, I never knew this.
Re:Deep Cloning Objects in JavaScript, the Modern Way
by cortesoft on Thursday December 21, 2023 @10:23PM
(#38730614)
You have it opposite… if you change the object after logging, it will retroactively change the logged version.
Re:Deep Cloning Objects in JavaScript, the Modern Way
by recursive on Thursday December 21, 2023 @11:49PM
(#38731053)
> You want to see what the object looks at at the time of logging
Yes. That's why you need to use structuredClone. If you don't, and you mutate the object, then you'll see the new values when you expand the object.
Chrome dev tools logs a pointer to the object, so if the object is mutated after logging, but before you view/expand it (especially for nested structures), it can be very confusing! JSON.parse(JSON.stringify()) usually is good enough for this :)
But yes, generally speaking I think a deep clone is a sign of a smell elsewhere. That doesn't mean it never has a place in any code, but just that if I find myself reaching for it, I usually see if there's a mistake being made elsewhere.