I'll break down the assignment and output of each variable as it plays out.
var foo = {n:2}
# foo: Object {n: 2}
# foo.x: undefined
foo.x = 3
# foo: Object {n: 2, x: 3 }
# foo.x: 3
foo.x = foo
# foo: Object {n: 2, x: Object {n:2, x: Object (recursive) } }
# foo.x: Object {n: 2, x: Object {n:2, x: Object (recursive) } }
foo.x = foo = {n: 2}
# foo: Object {n: 2}
# foo.x: undefined
The last line, as you can see, resets foo
to equal {n: 2}
which makes foo.x
non-existent (because you have overwritten the foo
object)