0

Something I don't get in the following snippet: a and b are now pointers to the same object.

var foo = {n:1}; /// foo points to an object
var bar = foo;  /// bar point to the same object as foo
foo.x = foo = {n:2};  /// foo is now pointing to a new object

In the last assignment, property x is added to bar. Why? Shouldn't x point to n:2?

0

1 Answer 1

0
foo.x = foo = {n:2};  /// foo is now pointing to a new object

you can simplify as

foo = {n:2}; // foo point to a NEW object
foo.x = foo;

so x is point to foo. property link to itself object.

Sign up to request clarification or add additional context in comments.

2 Comments

If I run this as script in Chrome, it appears that bar has new property. foo: Object n: 2 bar: Object n: 1 x: Object foo.x: undefined
This answer is wrong.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.