1.1 $.extend()
var OBJ1 = { 'A':. 1 }; var obj2 = { 'B':. 1 }; var C = $ .extend (OBJ1, obj2); the console.log (OBJ1); // {A:. 1, B: 1} obj1 has been modified // or var OBJ3 = $ .extend ({}, OBJ1, obj2) the console.log (OBJ3); // {A:. 1, B: not change. 1} obj1, obj21.2 traversal assignment
1.2 traversing assignment
var OBJ1 = { 'A':. 1 }; var obj2 = { 'B': 2, 'C':. 3 }; for ( var Key in obj2) { IF (obj2.hasOwnProperty (Key) === to true ) { // here is to determine its own hasOwnProperty properties, when used for in loop through properties of an object, all the properties of the prototype chain will be extended to access the object prototype would avoid the interference caused OBJ1 [Key] = obj2 [Key]; } } the console.log (OBJ1); // { 'A':. 1, 'B': 2, 'C':. 3};
1.3 Obj.assign()
It can be any number of source object itself may be enumerated attribute copy to the target object, the target object and then returns. Object.assign (target, ... Sources) // . A copy of an object obj var = {A <br>:. 1, B: 2}; var copyObj = Object.assign ({}, obj); the console.log (copyObj); // {A:. 1, B: 2} // B combine multiple objects. var O1 = {A:. 1 }; var O2 = {B: 2 }; var O3 C = {:. 3 }; var obj = Object.assign (O1, O2, O3); the console.log (obj); // {A:. 1, B: 2, C:}. 3 the console.log (O1); // {A:. 1, b: 2, c: 3} , and the target object itself will change.
Deep and shallow copy copy target 2
2.1 shallow copy
// shallow copy var OBJ1 = { 'A':. 1 }; var obj2 = { 'B': { 'B1': 22 is, 'B2': 33 is }}; $ .extend (OBJ1, obj2); // OBJ1 obj2 copy attribute of the console.log (OBJ1) // { 'a':. 1, 'B' { 'B1': 22 is, 'B2':}} 33 is the console.log (obj1.b.b1) // 22 is obj2.b.b1 = 44 is; // obj2 reassign the console.log (obj1.b.b1) // 44 is only copied obj1.b guide object, it is affected by the original obj2
2.2 deep copy
// deep copy var OBJ1 = { 'A':. 1 }; var obj2 = { 'B': { 'B1': 22 is, 'B2': 33 is }}; $ .extend ( to true , OBJ1, obj2); / / The first deep copy is set to true indicates the console.log (OBJ1) // { 'a':. 1, 'B' { 'B1': 22 is, 'B2':}} 33 is the console.log (obj1.b .b1) // 22 is obj2.b.b1 = 44 is; // reassigned obj2 the console.log (obj1.b.b1) // 22 is OBJ1 copy all attributes and values obj2 is not affected by the obj2