var extendDeep = function(parent,child){
	
	var i,
		toStr = Object.prototype.toString,
		astr  = '[object Array]';
	child = child || {};

	for( i in parent){
		if(parent.hasOwnProperty(i)){
			if(typeof parent[i] === 'object'){
				child[i] = toStr.call(parent[i])=== astr ? [] : {};
				extendDeep(parent[i],child[i]);
			}else{
				child[i] = parent[i];
			}
			
		}
	}
	
	return child;
};
//测试代码:
var obj1 = {
	a : 1,
	b : {
		c : {
			d : 1
		}
	}
};
var obj2 = extendDeep(obj1);
console.log(obj2);

一个简单的javascript深拷贝

相关文章:

  • 2022-02-10
  • 2021-12-15
  • 2021-12-22
  • 2022-01-15
猜你喜欢
  • 2022-12-23
  • 2021-07-21
  • 2021-06-19
  • 2021-08-01
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案