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?

badigard
  • 820
  • 2
  • 10
  • 22

1 Answers1

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.

Dmitriy
  • 3,745
  • 16
  • 24