typescript深度拷贝陷入死循环了,该怎么修改代码?求解!谢谢

deepCopy(data: any) {

let t = this.type(data),
  o: any,
  i,
  ni;
if (t === "array") {
  o = []; //初始化
} else if (t === "object") {
  o = {}; //初始化
} else {
  return data;
}
if (t === "array") {
  //console.log("t:%o,data:%o", t, data);
  for (i = 0, ni = data.length; i < ni; i++) {
    o.push(this.deepCopy(data[i]));
  }
  return o;
} else if (t === "object" ) {
  //console.log("t:%o,data:%o", t, data);
  for (i in data) {
    if(data[i]===data){
      continue;
    }
    o[i] = this.deepCopy(data[i]);
  }
  return o;
}

}
type(obj: any) {

var toString = Object.prototype.toString;
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"
};
return map[toString.call(obj)];

}

阅读 2.1k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
Microsoft
子站问答
访问
宣传栏