Two destinations on one line

Let's start with this:

var foo = {n:1}; var bar = foo;

In JavaScript, what is the order in which the following assignments are performed:

foo.x = foo = {n:2};

When I raised the answers to this, I found a few that seemed to suggest that assignments are made from RIGHT LEFT, for example:

foo = {n:2}; foo.x = foo;

In this case, we expect:

console.log(foo) //{n:2, x:Object} - (a circular reference back to foo)

However, what I see in the console when I really do this suggests that the assignments really come from LEFT TO RIGHT:

console.log(foo) //{n:2} console.log(bar) //{n:1, x:Object} -> Object {n: 1} obj gets the 'x' property, and foo is reassigned to the new object {n: 2}

Can someone clarify for me the order of operations when there are several assignments in one line?

In addition, which is the basic rule that distinguishes the above scenario from a situation similar to the following: Multiple variable assignments on the same line

, ...

+4
2

. , foo.x . ( Ecma 11.13.1 )

. foo bar . ( inMemoryObject).

foo --|
bar --|
      |---> inMemoryObject { 
                               n:1 
                           }

foo.x . foo.x , , .

foo --|
bar --|
      |---> inMemoryObject { 
                               n:1, 
                               x: undefined 
                           }

, inMemoryObject.x. foo inMemoryObject, bar .

bar --|
      |---> inMemoryObject { 
                              n:1, 
                              x: --| 
foo --|                     }      |
      |                            |
      |----------------------------|--- secondInMemoryObject {
                                                                n:2
                                                             }
+12

, .

, . , .

+1

All Articles