1.json.parse 但是不支持方法拷贝
2.递归
var x = { a : {e : 6},b : 2,arr : [1,2,3],fun : function(){return 8}}
function clone(x){if(x.constructor === Object){var obj = {}for(var k in x){obj[k] = clone(x[k])}return obj}else if(x.constructor === Array){var arr = [];for (var i = 0; i < x.length; i++) {arr[i] = clone(x[i])}return arr}else {return x}}
var y = clone(x)
3.函数库lodash
该函数库也有提供 _.cloneDeep 用来做 Deep Copy。
网友评论