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
子站问答
访问
宣传栏