//https://blog.csdn.net/weixin_34326558/article/details/93181568
var parents = [];//专门用来记录parent里出现循环引用的位置
var children = [];//记录已被克隆的内容
var clone = (parent)=>{
if(parent == null)return null;
if(typeof parent !== 'object'){return parent;}
let child,proto;
if(isType(parent,'Array')){
child = [];
}else if(isType(parent,'Function')){
child = parent;
return child;
}else if(isType(parent,'Date')){
child = new Date(parent.getTime());
return child;
}else if(isType(parent,'RegExp')){
child = new Date(parent.source,getRegExp(parent));
return child;
}else{
proto = Object.getPrototypeOf(parent);
child = Object.create(proto);
}
//处理循环引用、递归
let index = parents.indexOf(parent);
if(index != -1){
//递归过程中发现循环引用,这个引用一定是之前递归里已经克隆的内容,存在了children中
return children[index];
}
parents.push(parent);
children.push(child);
for(let i in parent){
//let in语法,对数组、对象都适用
child[i] = clone(parent[i]);
}
return child
}
console