const deepCopy = (obj) => {
if (typeof obj !== 'object' || obj === null) {
return obj;
}
let copy = Array.isArray(obj) ? [] : {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
copy[key] = deepCopy(obj[key]);
}
}
return copy;
};
// 使用示例
const originalObj = { name: 'John', age: 30, hobbies: ['reading', 'coding'] };
const copiedObj = deepCopy(originalObj);