var a=[{a:1,b:2},[1,2,3,4],function(){}];
var b={
c:[1,2,3],
d:{
c:1,
l:2
}
};
有什么办法可以写一个方法深拷贝这种例子。
JSON.parse(JSON.stringify(obj))
见代码
function clone(obj) {
var objType = Object.prototype.toString.call(obj);
if(objType === '[object Array]' || objType === '[object Object]') {
var temp = (objType === '[object Array]') ? [] : {} ;
for(var k in obj) {
temp[k] = arguments.callee(obj[k]);
}
return temp;
}
return obj;
}
var a=[{a:1,b:2},[1,2,3,4],function(){}];
var b={
c:[1,2,3],
d:{
c:1,
l:2
}
};
var o = clone(b);
var oo = clone(a);
console.log(o);
console.log(oo);