JavaScript通过递归实现深拷贝
思路
首先是用Object.prototype.toString.call(obj)
来得到传入的值的类型,如果是几个基本类型,则直接返回值就可以了
如果是引用类型,则通过深拷贝函数递归进行再次拷贝。
注:也可以用constructor判断类型(参见deepClone2函数)
代码
/** * 实现深拷贝 * @param {*} obj * @returns {*} 拷贝完的对象 */ function deepCopy(obj) { let type = Object.prototype.toString.call(obj); type = type.split(' ')[1].split(']')[0]; console.log(type); if (type === 'Number') { return obj; } else if (type === 'Object') { const new_obj = {}; for (item in obj) { if (typeof item === 'object') { new_obj[item] = deepCopy(item); } else { new_obj[item] = obj[item]; } } return new_obj; } else if (type === 'Function') { return obj; } else if (type === 'String') { return obj; } else if (type === 'Boolean') { return obj; } else if (type === 'Array') { const arr = []; for (item of obj) { if (typeof item === 'object') { arr.push(deepCopy(item)); } else { arr.push(item); } } return arr; } else if (type === 'Null') { return null; } } const obj = { a: 1, b: { bb: 1 }, d: [1, 2, { cc: '123' }] } // const copy_obj = deepCopy(obj); // console.log(obj === copy_obj) // console.log(copy_obj); // console.log(deepCopy(null)) /** * 深拷贝优化版 * * @param {*} source * @returns {*} */ function deepClone2(source) { let target; console.log(source.constructor); if (source.constructor === Array || source.constructor === Object) { target = source.constructor === Array ? [] : {}; for (let key in source) { if (source.hasOwnProperty(key)) { if (source[key] && typeof source[key] === 'object') { target[key] = deepClone2(source[key]); } else { target[key] = source[key]; } } } } else { target = source; } return target; } console.log(deepClone2(function () { })) console.log(deepClone2([])) console.log(deepClone2(1)) console.log(deepClone2(obj))
分类:
前端面试题
标签:
JavaScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了