【转】数组、对象的深拷贝

链接:https://www.zhihu.com/question/23031215/answer/31944721

var cloneObj = function(obj){
	var str, newobj = obj.constructor === Array ? [] : {};
	if(typeof obj !== 'object'){
		return;
	} else if(window.JSON){
    	str = JSON.stringify(obj), //系列化对象
    	newobj = JSON.parse(str); //还原
	} else {
    	for(var i in obj){
		    newobj[i] = typeof obj[i] === 'object' ? 
		    cloneObj(obj[i]) : obj[i]; 
	    }
    }
    return newobj;
};

posted on 2017-10-13 16:14  xfh0192  阅读(71)  评论(0编辑  收藏  举报

导航