javascript - A problem with assignment in JS
过去多啦不再A梦
过去多啦不再A梦 2017-05-18 10:51:04
0
9
714

We all know that assignment in javascript is from right to left

var a = b = c

The above statement is actually executed like this

b = c
var a = b

But what if there is an object involved? An example I saw yesterday

var foo = { a: 2 }
var bar = foo 
foo.x = foo = { b:  3 }

The first two lines are very simple, just point bar to foo.
In the third line, foo = { b: 3 } is executed at the beginning, which redirects foo to another object.

The next sentence foo.x = foo, I originally thought that foo.x points to foo, so that foo becomes an object that cyclically refers to itself, that is,

foo = {
    b: 3,
    x: {
        b: 3,
        x: {
            b: 3,
            x: {
                ......
            }
        }
    }
}

However, the actual test found that foo.x in the sentence foo. Right, but the actual execution will be from right to left?

过去多啦不再A梦
过去多啦不再A梦

reply all(9)
PHPzhong

I just saw a good explanation from a great master (I am checking the information). The approximate explanation of continuous assignment of objects is as follows: foo.x = foo = { b: 3 }, first during the execution of the interpreter See if foo.x and foo have been created. If not, create it and point them both to the rightmost object. Obviously foo is created and points to { a: 2 }, foo.x has not been created yet, so create it and let it Point to null. At this time, bar and foo both point to {a:2,x:null}, and then point them both to the object {b:3} on the right. At this time, bar points to {a:2,x:{b:3}. },foo points to {b:3};

Ty80

We just discussed this issue recently, and we came to the conclusion that: foo.x 中的 foo 实际是对 { a: 2 } is a reference, but the execution order is still from right to left, there is no doubt about this.

var foo = { a: 2 }
var bar = foo.a = 3
console.log(bar) //3

var foo1 = { a: 2}
var bar1 = foo1.a
foo1.a = 3
console.log(bar1) //2
漂亮男人

Assignment is an expression,
Associativity is from right to left, that is, a = b = c is a = (b = c)a = b = ca = (b = c)
返回值是,等号右边那个表达式的返回值,即 b = c 的返回值是cThe return value is, etc. The return value of the expression on the right side of the number, that is, the return value of b = c is the value of c.

仅有的幸福

Local left to right (.), macro right to left (=). This foo is not that foo .

foo.x = foo = { b:  3 }
When

reads the second foo, it has already obtained the reference of { a: 2 } from the first foo and is waiting to assign a value to its x attribute.

滿天的星座

Assignment is from right to left, but the code must be processed before execution. foo.x = foo = { b: 3 },对属性的处理是按值进行的,即此时已经定位到了{ a: 2 }The actual storage location of this object. The assignment to attribute x is also the assignment to the x attribute of the actual object, whether it is the object pointed to by foo or bar The object pointed to. Assignment to an object changes the address of the actual object stored in the object, that is, changes the pointer of foo.

小葫芦

I don’t think this way of writing makes sense.
In actual production, if you encounter a lot of such code, can you understand it at a glance?
Just like now, how many of the people who answered the previous questions can explain it in layman’s terms?

迷茫

/a/11...

刘奇
var a=b=c;

b=c;//c
a=c;
foo.x = foo = { b:  3 }

foo = { b:  3 };//返回值为 { b : 3 } 的指针,下同
foo.x = { b : 3 }的指针;

But actually executing the above three statements in order, the results will be different, because foo.x has been prepared during the parsing stage, so it actually points to bar.x

Actual equivalent code

foo.x = foo = { b:  3 }

foo = { b:  3 };//{ b : 3 }
bar.x = { b : 3 }的指针;
洪涛

You can use pointers to understand this problem

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template