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

推荐订阅源

博客园 - 三生石上(FineUI控件)
月光博客
月光博客
人人都是产品经理
人人都是产品经理
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
Vercel News
Vercel News
MyScale Blog
MyScale Blog
爱范儿
爱范儿
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
H
Help Net Security
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
L
LangChain Blog
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
宝玉的分享
宝玉的分享
博客园 - 聂微东
云风的 BLOG
云风的 BLOG
J
Java Code Geeks
博客园 - 叶小钗
D
Docker

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