Home > front end >  Some doubt about shallow copy, hope leaders help hard to answer.
Some doubt about shallow copy, hope leaders help hard to answer.

Time:04-30

 
<script>
Var obj={
Id: 1,
Name: haha,
Ob: {
Id: 23
}
}
Var o={}
For (var k in obj) {
//obj [k] said attribute values, k said the property name
//o [k] said what, also said the property value? That is how to copy?
O [k]=obj [k].
}
O.i d=5;
O.o b={
Id: 66
}
The console. The log (o)
The console. The log (obj)

The above code blocks to shallow copy and copy to o object obj, not to say that the shallow copy just copy the address, professional call copy the reference, change after one of the objects in the complex data types, the relevant data from another object will change, but why the following code changes the complex data type in one of the objects, the other types of complex data in an object is not affected, for example, changing the o.o b data but obj. Ob is not affected,
 
O.o b={id: 66}

Below is change o.o b={id: 66} the running result of the


Unless the change, such as the following obj. Data will only be affected in ob, why is this?
 
O.o b.i d=66;

Hope bosses advice, thank you.
  • Related