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)];
}
果然是循环引用引起的问题,看了https://segmentfault.com/a/11...,谢谢作者的分享!