5 ms·
I was just about to mention this, I wasted a solid hour or two on this until I realised what was happening.
by matja 6y ago
I was just about to mention this, I wasted a solid hour or two on this until I realised what was happening.
- brundolf 6y agoAs I pointed out in another comment: console.log({x, y}); the wrapping object is being created at log time, so its values will never be changed after the fact. That could still happen with the contents of x or y themselves, but then it's no different from the original way (console.log(x, y);)
- evan_ 6y agoWe can actually just test this. Here's some code: const x = {value: 0}; console.log(x); console.log({x}); x.value = 1; Running that in the latest Chrome javascript console, we see that the first version prints `{value: 0}` and the second prints `{x: {...}}`. When you expand the second one, it will show `{x: {value: 1}}`.
- Lex-2008 6y agoyep, but then when I expand (click triangle) the {value: 0} line, it shows "value: 1" on the next line, only to rise the confusion. To be fair, there is also an "i" in a square, reminding me about this behaviour.
- Folcon 6y agoThe really fun part is doing this :)... Evaluate the expression: const x = {value: 0}; console.log(x); console.log({x}); x.value = 1; You get this: {value: 0} {x: {…}} Expand the first arrow of the `x:`: v {x: {…}} > x: {value: 1} Now evaluate: x.value = 3 Then expand the second arrow: v {x: {…}} > x: value: 3 Now if you unexpand the arrow, you get 1, but if you expand it you get 3 =)... (Well at least in chrome)
- Lex-2008 6y ago> Now if you unexpand the arrow, you get 1, but if you expand it you get 3 =)... Moreover, if you expand arrow next to {value: 0}, you will see literally this: v {value: 0} [i] value: 3
- brundolf 6y ago> its values will never be changed after the fact By this I mean, the x and y in the output will never themselves change value. They may be mutated, but they cannot be reassigned in the printed object. The printed object is exclusively referenced by the console itself, even if the nested objects within it may be referenced elsewhere. > That could still happen with the contents of x or y themselves, but then it's no different from the original way (console.log(x, y);) By this I mean exactly what you demonstrated, the point being that it had nothing to do with the original suggestion made by jchw.