2

I have this kind of construction

let a = {x: 1}
let b = a
a.x = a = {y: 2}

How you can explain in details how I get these results ?

a.x = undefined
a={y: 2}
b={x: {y: 2}}
6
  • You've just overriden a with an object that only contains the property y. a = {y: 2} - You can see this when you console.log a.y. These assignments are done right to left. Commented Apr 4, 2019 at 12:57
  • a.x is evaluated (but not yet written) before a = {y:2} Commented Apr 4, 2019 at 13:00
  • The very last assignment that occurs here is a = { y: 2 };. This clobbers a.x. Perhaps you were expecting the opposite order of assignments on the last line? Commented Apr 4, 2019 at 13:00
  • @Lewis you just explain me only one case. I understand why this happens , but I can't explain it to others in clear way ) Commented Apr 4, 2019 at 13:04
  • 1
    This has useful answers on the order: Why do these snippets of JavaScript behave differently even though they both encounter an error? Commented Apr 4, 2019 at 13:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.