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

推荐订阅源

G
Google Developers Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
V
Visual Studio Blog
Martin Fowler
Martin Fowler
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 叶小钗
I
InfoQ
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
P
Proofpoint News Feed
WordPress大学
WordPress大学
小众软件
小众软件
B
Blog
MongoDB | Blog
MongoDB | Blog
人人都是产品经理
人人都是产品经理
量子位
Hugging Face - Blog
Hugging Face - 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)  评论()    收藏  举报