const isObject = obj => Object.prototype.toString(obj) === '[object Object]';
const isArray = array => Array.isArray(array);
let human = {
name: 'kodo',
age: 18,
work1: ['web', 'student'],
hobby: {
game: ['sc2', 'dota2']
}
};
function deepClone(target) {
if (target === null || typeof target !== "object") {
return target;
}
let newTarget = isArray(target) ? [] : {};
for ( let key in target ) {
if (target.hasOwnProperty(key)) {
let value = target[key];
if (value === target) {
continue;
}
if (isObject(value) || isArray(value)) {
newTarget[key] = deepClone(value)
} else {
newTarget[key] = value;
}
}
}
return newTarget;
}
var a = deepClone(human);
// console.log(a);
a.name = 'sc222';
console.log(human, '---human---');
console.log(a, '---a---');