I don't understand the output of this code. Ask the expert to help me analyze it.

    let a = {n: 1}
    let b = a;
    a.x = a = {n: 2}
    console.log(a.x);
    console.log(b.x);

the second output b.x I can understand, because an and b are the same reference,

but I really don"t understand the output of one. Ask God to help me analyze

.
Nov.20,2021

https://codeshelper.com/a/11.


axi5 is an expression that returns a value

>  a=5
<  5

so b=a=5 is assigned from right to left.
then a.x = a = {n: 2} is equivalent to

a = {n:2} // -> a : {n:2}
a.x = a   // -> a : {n:2,x:{n:2}}

that's about what it means.



    [][1]


let a = {n: 1} //
let b = a;  //  
a.x = a = {n: 2} //
   --  a = {n: 2} //a
   --  a.x = a // {n: 2}; a
console.log(a.x); //a
console.log(b.x); //b
//aa

understand the order in which the js engine reads connected equations

Menu