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

推荐订阅源

博客园 - 三生石上(FineUI控件)
月光博客
月光博客
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
宝玉的分享
宝玉的分享
A
About on SuperTechFans
Vercel News
Vercel News
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
Jina AI
Jina AI
Y
Y Combinator Blog
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI

博客园 - 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)  评论()    收藏  举报