const clone = function (myObj) {
if (typeof myObj !== 'object') return myObj;
if (myObj === null) return myObj;
if(myObj instanceof Array){
var myNewObj = [];
for (var i in myObj) {
if (myObj[i] instanceof Array) {
var newArr = [];
for (var j = 0; j < myObj[i].length; j++) {
newArr.push(clone(myObj[i][j]));
}
myNewObj[i] = newArr;
} else {
myNewObj[i] = clone(myObj[i]);
}
}
return myNewObj;
}else{
var myNewObj = {};
for (var i in myObj) {
if (myObj[i] instanceof Array) {
var newArr = [];
for (var j = 0; j < myObj[i].length; j++) {
newArr.push(clone(myObj[i][j]));
}
myNewObj[i] = newArr;
} else {
myNewObj[i] = clone(myObj[i]);
}
}
return myNewObj;
}
};
let json = {a:'a',b:'b',c:'c',d:'d'}
let newJson = clone(json);
newJson['e']='e';
console.log(json)
console.log('复制之后newJSON:')
console.log(newJson)