no copy of the situation
var obj = {
a:10
};
var obj2 = obj;
obj2.a =;
alert (OBJ.A); ==> //assignment will directly modify the value of OBJ.A
deep copy and shallow copy of JavaScript
A shallow copy of JavaScript
Shallow copies replicate only the properties of an object in turn, and do not replicate recursively, and JavaScript storage objects are stored addresses, so shallow replication causes obj.arr and Shallowobj.arr to point to the same memory address
var obj = {
a:10
}
function copy (obj) {
var newObj = {};
for (var attr in obj) {
newobj[attr] = obj[attr];
}
return newObj;
}
var obj2 = copy (obj);
obj2.a =;
alert (OBJ.A); ==> 10
Second, the problem of the shallow copy of JavaScript
var obj = {
a:{
b:10
}
}
function copy (obj) {
var newObj = {};
for (var attr in obj) {
newobj[attr] = obj[attr];
}
return newObj;
}
var obj2 = copy (obj);
OBJ2.A.B =;
alert (OBJ.A.B); ==>20
deep copy of JavaScript
First, deep copy need to consider the concept of recursion
Deep copy not only copies the properties of the original object individually, but also recursively copies the objects contained in the original object to the new object, in turn, by using the deep copy method. This does not present the problem that the Arr attribute of obj and shallowobj above points to the same object.
recursive function (factorial) function
Digui (n) {
if (n==1) {return
1;
}
Return N*digui (n-1);
}
Alert (Digui (5));
Second, deep copy
var obj = {
a:{
b:10
}
}
function deepcopy (obj) {
if (typeof obj!= ' object ') {
return obj;
}
var newObj = {};
for (var attr in obj) {
newobj[attr] = deepcopy (obj[attr));
return newObj;
}
var obj2 = deepcopy (obj);
OBJ2.A.B =;
alert (OBJ.A.B); ==>10
deep copy and shallow copy of jquery
Deep and shallow copies of $.extend in JQ
var a = {};
var b = {name: {age:30}};
Shallow copy
$.extend (A, b);
Join parameter true is deep copy
$.extend (True, A, b);