惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Security Blog
Microsoft Security Blog
Jina AI
Jina AI
量子位
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
S
SegmentFault 最新的问题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
雷峰网
雷峰网
博客园 - 聂微东
美团技术团队
Last Week in AI
Last Week in AI
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园_首页
V
Visual Studio Blog
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog

博客园 - howhy

TypeScript any vs unknown 详细对比 TypeScript interface vs type 完整对比 html 元素包含关系 this 指向 空对象 Object.keys for ...in Reflect.ownKeys ...区别 逻辑运算符和空值运算符 运算符优先级 js 类型显式转换 js 高级函数 js 方法重载 fetch timeout js 任务顺序执行 暂停 js 并发任务 判断两个对象是否相同 js 动态拦截属性 js 通用动画 js groupby js 防抖和节流 实现 instanceof 操作符 js 单例模式 js 数组去重和扁平方法 js 继承方法 js new的过程实现 js '=='的隐性类型转换规则
js deepCopy
howhy · 2025-12-25 · via 博客园 - howhy
function deepCopy(obj){
    if(obj===null || obj===undefined || typeof obj!=='object'){
        return obj;
    }
    if(Array.isArray(obj)){
        return obj.map(item=>deepCopy(item));
    }
    if(obj instanceof Object){
        const newObj={};
        for(let key in obj){
            if(obj.hasOwnProperty(key)){
                newObj[key]=deepCopy(obj[key])
            }
        }
        return newObj;
    }
    return obj;
}
// 进阶优化:解决循环引用 + 支持更多类
function deepCopy(obj, cache = new WeakMap()) {
    // 递归终止条件
    if (obj === null || obj === undefined || typeof obj !== 'object') {
        return obj;
    }

    // 若已拷贝过该对象,直接返回缓存的副本(解决循环引用)
    if (cache.has(obj)) {
        return cache.get(obj);
    }

    let newObj;

    // 处理 Date 类型
    if (obj instanceof Date) {
        newObj = new Date(obj);
        cache.set(obj, newObj);
        return newObj;
    }

    // 处理 RegExp 类型
    if (obj instanceof RegExp) {
        newObj = new RegExp(obj.source, obj.flags);
        cache.set(obj, newObj);
        return newObj;
    }

    // 处理 Array 类型
    if (Array.isArray(obj)) {
        newObj = [];
        cache.set(obj, newObj);
        obj.forEach((item, index) => {
            newObj[index] = deepCopy(item, cache);
        });
        return newObj;
    }

    // 处理 Map 类型
    if (obj instanceof Map) {
        newObj = new Map();
        cache.set(obj, newObj);
        obj.forEach((value, key) => {
            newObj.set(deepCopy(key, cache), deepCopy(value, cache));
        });
        return newObj;
    }

    // 处理 Set 类型
    if (obj instanceof Set) {
        newObj = new Set();
        cache.set(obj, newObj);
        obj.forEach(value => {
            newObj.add(deepCopy(value, cache));
        });
        return newObj;
    }

    // 处理普通对象
    if (obj instanceof Object) {
        newObj = {};
        cache.set(obj, newObj); // 先缓存,避免递归中再次引用当前对象
        for (let key in obj) {
            if (obj.hasOwnProperty(key)) {
                newObj[key] = deepCopy(obj[key], cache);
            }
        }
        return newObj;
    }

    return obj;
}

posted @ 2025-12-25 15:25  howhy  阅读(33)  评论()    收藏  举报