Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

No, this is totally different. In the second version, if someObj changes after it was logged, when you'll expand it you'll see the updated value. JSON.stringify freezes the value. To get the same as the first example, but interactive, you have to do:

  console.log("some label: " + JSON.parse(JSON.stringify(someObj)))


> you have to do

Technically you'd have to do

  console.log("some label: ", JSON.parse(JSON.stringify(someObj)))
> In the second version, if someObj changes after it was logged, when you'll expand it you'll see the updated value

Yes, this is something to be aware of (and is getting beaten to death throughout this comments section), but if like me you mostly use plain objects in an immutable way, you generally don't have to bother with cloning. Just keep this in the back of your head and know when it won't do what you want in a particular context.


> Just keep this in the back of your head

I would never debug with that mindstate. I don't trust myself.


It's only different if you don't treat objects as immutable.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: