深度克隆和浅拷贝

https://www.cnblogs.com/echolun/p/7889848.html

https://www.cnblogs.com/jieker/p/8946213.html

https://zhuanlan.zhihu.com/p/61340256

 

// 判断变量的类型
function getType(obj){
var str = Object.prototype.toString.call(obj);
var map={
'[object Boolean]' : 'boolean',
'[object Number]' : 'number',
'[object String]' : 'string',
'[object Function]' : 'function',
'[object Array]' : 'array',
'[object Date]' : 'date',
'[object RegExp]' : 'regExp',
'[object Undefined]': 'undefined',
'[object Null]' : 'null',
'[object Object]' : 'object'
}
if(obj instanceof Element){ //判断是否是dom元素,如div等
return "element";
}
return map[str];
}

//深拷贝函数
function deepCopy(p){
var obj;
var str = getType(p);
if(str == 'array'){
obj = [];
for (var i = 0; i < p.length; i++) {
obj.push(arguments.callee(p[i])); //回调自己
}
}else if(str == 'object'){
obj = {};
for(var i in p){
obj[i] = arguments.callee(p[i]);
}
}else{
return p;
}
return obj;
}
 上面的克隆方法还是有很多问题:https://blog.csdn.net/weixin_36774307/article/details/110230396?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7-110230396-blog-120695412.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7-110230396-blog-120695412.pc_relevant_default&utm_relevant_index=9

posted @ 2019-04-06 22:12  大哥成  阅读(122)  评论(0编辑  收藏  举报