One: Shallow copy
var m = {a:10, b:20}
var n = m;
N.A = 15;
M.A will output 15 because this is a shallow copy, where N and M point to the same heap, the object copy is just a reference to the copied object.
Implementation method
function Simpleclone (initalobj) {
var obj = {};
for (var i in initalobj) {
Obj[i] = Initalobj[i];
}
return obj;
}
Object.assign () implementation
The Object.assign () method can copy an enumerable property of any number of source objects to the target object, and then return to the
The target object. But Object.assign () is a shallow copy, copying a reference to an object's properties, not the object itself.
。
var obj = {A: {a: "Hello", b:21}};
var initalobj = Object.assign ({}, obj);
INITALOBJ.A.A = "Changed";
Console.log (OBJ.A.A); "Changed"
One: Deep copy
var m = {a:10, b:20}
var n = {A:m.a,b:m.b};
N.A = 15;
This time, we will output M.A, found that the value of M.A is still 10, and has not changed, M object and N object is although all the values are
The same, but in the heap, the corresponding is not the same, this is a deep copy.
Implementation method
When object has only one layer, it is a deep copy
var obj1 = {a:10, b:20, c:30};
var obj2 = Object.assign ({}, obj1);
obj2.b = 100;
Console.log (OBJ1);
{a:10, b:20, c:30} <--has not been changed to
Console.log (OBJ2);
{a:10, b:100, c:30}
When multilayer:
var cloneobj = function (obj) {
var str, newobj = Obj.constructor = = = Array? [] : {};
if (typeof obj!== ' object ') {
Return
} else if (window. JSON) {
str = json.stringify (obj),//serialized object
newobj = Json.parse (str); Restores
} else {
for (var i in obj) {
Newobj[i] = typeof obj[i] = = = ' object '?
Cloneobj (Obj[i]): Obj[i];
}
}
return newobj;
};
Use Json.stringify to turn the object into a string, and then use Json.parse to turn the string into a new object.
The following functions can be encapsulated
var cloneobj = function (obj) {
var str, newobj = Obj.constructor = = = Array? [] : {};
if (typeof obj!== ' object ') {
Return
} else if (window. JSON) {
str = json.stringify (obj),//serialized object
newobj = Json.parse (str); Restores
} else {
for (var i in obj) {
Newobj[i] = typeof obj[i] = = = ' object '?
Cloneobj (Obj[i]): Obj[i];
}
}
return newobj;
};
Recursive copy
function Deepclone (initalobj, finalobj) {
var obj = Finalobj | | {};
for (var i in initalobj) {
var prop = Initalobj[i]; Avoid cross-referencing of objects resulting in a dead loop, such as INITALOBJ.A = Initalobj case
if (prop = = = obj) {
Continue
}
if (typeof prop = = = = ' object ') {
Obj[i] = (Prop.constructor = = = = Array)? [] : {};
Arguments.callee (prop, Obj[i]);
} else {
Obj[i] = prop;
}
}
return obj;
}
var str = {};
var obj = {A: {a: "Hello", b:21}};
Deepclone (obj, str);
Console.log (STR.A);
Using the Object.create () method
Using var newObj = Object.create (oldobj) directly, you can achieve the effect of deep copy.
function Deepclone (initalobj, finalobj) {
var obj = Finalobj | | {};
for (var i in initalobj) {
var prop = Initalobj[i]; Avoid mutually referencing objects that cause a dead loop, such as INITALOBJ.A =
The situation of Initalobj
if (prop = = = obj) {
Continue
}
if (typeof prop = = = = ' object ') {
Obj[i] = (Prop.constructor = = = = Array)? []: Object.create (prop);
} else {
Obj[i] = prop;
}
}
return obj;
}
jquery has provided a $.extend that can be used to do deep Copy.
var $ = require (' jquery ');
var obj1 = {
A:1,
B: {f: {g:1}},
C: [1, 2, 3]
};
var obj2 = $.extend (true, {}, obj1);
Console.log (Obj1.b.f = = = Obj2.b.f);
False
Lodash
Another very popular function library Lodash, also provides _.clonedeep to do deep Copy.
var _ = require (' Lodash ');
var obj1 = {
A:1,
B: {f: {g:1}},
C: [1, 2, 3]
};
var obj2 = _.clonedeep (obj1);
Console.log (Obj1.b.f = = = Obj2.b.f);
False
JS deep copy and shallow copy