let arr = [1, 2, 3, { 'name': 'tom' }];
// let arr1 = arr;
// let arr2 = arr.slice();
// console.log(arr1);
// console.log(arr2);
// arr[0]=999;
// arr[3].name = 'jerry';
// console.log(arr1);
// console.log(arr2);
function shallow(target) {
let cloneTarget = (Array.isArray(target)) ? [] : {};
for (key in target) {
cloneTarget[key] = target[key];
}
return cloneTarget;
}
function deep(target) {
let cloneTarget = (Array.isArray(target)) ? [] : {};
for (key in target) {
if(target[key] instanceof Object){
cloneTarget[key] = deep(target[key]);
} else
cloneTarget[key] = target[key];
}
return cloneTarget;
}
arr1 = shallow(arr);
arr2 = deep(arr);
arr3 = JSON.parse(JSON.stringify(arr));
arr[0] = 999;
arr[3].name = 'jerry';
console.log(arr1);
console.log(arr2);
console.log(arr3);
console