Using jquery's extend approach:
extend ([param1,]obj1,obj2);
Description:1.> Copying an object obj2 to Obj1
2>PARAM1, which is a Boolean type, True indicates a deep copy
eg
- var Object1 = {
- Apple : 0,
- Banana : {weight: *, price: +},
- Cherry :
- };
- var Object2 = {
- Banana : {price: +},
- Durian :
- };
- $. Extend (True,object1, object2); //Deep copy, to be copied with a new object, Object1 can be written as {}
- object2. Banana . Price =100; //Modify Values
- Alert (object1. Banana . Price )//Deep copy, the value in Object1 does not change
JavaScript implementations:
- function Cloneobject (obj) {
- var clone = {};
- for (var i in obj) {
- if (typeof(obj[i]) == "Object")
- clone[i] = cloneobject(obj[i]);
- Else
- Clone [i] = obj[i];
- }
- return clone;
- }
eg
- function Cloneobject (obj) {
- var clone = {};
- for (var i in obj) {
- if (typeof(obj[i]) == "Object")
- var Object1 = {
- Apple : 0,
- Banana : {weight: *, price: +},
- Cherry :
- };
- var obj= cloneobject (object1 );
- Object2 . Banana . Price =200; //Modify Values
- alert (obj. Banana . Price) //Output still
JQuery Deep-clone JavaScript objects