A note about javascrip reference objects

If a = {x: {}}

Set obj = ax;

Then when x changes. obj has not changed.

Example node operation:

var a = {x: {name: 'hello'}}
// undefined
var obj = a.x
// undefined
obj
// {name: "hello"}
a.x = {m: 1, n: 2}
// {m: 1, n: 2}
obj
// {name: "hello"}

obj is the reference address of ax, ax changed to point, ovj still points to the assigned address. Hope to change, directly refer to the object itself: obj = a, or modify the object properties;

var b = {x: {name: 'hello'}}
// undefined
var obj2 = b.x
// undefined
obj2
// {name: "hello"}
b.x.name = 'world'
// "world"
obj2
// {name: "world"}

 

Guess you like

Origin blog.csdn.net/u013475983/article/details/103522376