JS深拷贝

  1. WeakMap常规遍历递归
function deepClone(source, map = new WeakMap()) { // 传入一个WeakMap对象用于记录拷贝前和拷贝后的映射关系
    if (typeof source !== "object") { // 非对象类型(undefined、boolean、number、string、symbol),直接返回原值即可
        return source;
    }
    if (source === null) { // 为null类型的时候
        return source;
    }
    if (source instanceof Date) { // Date类型
        return new Date(source);
    }
    if (source instanceof RegExp) { // RegExp正则类型
        return new RegExp(source);
    }
    if (map.get(source)) { // 如果存在相互引用,则从map中取出之前拷贝的结果对象并返回以便形成相互引用关系
        return map.get(source);
    }
    let result;
    if (Array.isArray(source)) { // 数组
        result = [];
        map.set(source, result); // 数组也会存在相互引用
        source.forEach((item) => {
            result.push(deepClone(item, map)); // 必须传入第一次调用deepClone时候创建的map对象
        });
        return result;
    } else { // 为对象的时候
        result = {};
        map.set(source, result); // 保存已拷贝的对象
        const keys = [...Object.getOwnPropertyNames(source), ...Object.getOwnPropertySymbols(source)]; // 取出对象的key以及symbol类型的key
        keys.forEach(key => {
            result[key] = deepClone(source[key], map); // 必须传入第一次调用deepClone时候创建的map对象
        });
        return result;
    }
}
  1. js原生apistructuredClone structuredClone() - Web API 接口参考 | MDN (mozilla.org)
// structuredClone()
const m = {
    a: 1,
    date: new Date(),
    // e: function foo (i) { i*10 },
    // error: new Error('error message')
    c: undefined,
    d: null,
    reg: /^1[3-9]\d{10}$/
}

const n = {
    p: m
}

m.b = n

const r = structuredClone(m)
console.log(r)

限制

  • 不允许克隆ErrorFunctionDOM对象,如果对象中含有,将抛出DATA_CLONE_ERR异常。
  • 不保留 RegExp 对象的 lastIndex 字段。
  • 不保留属性描述符,setters 以及 getters(以及其他类似元数据的功能)。例如,如果一个对象用属性描述符标记为 read-only,它将会被复制为 read-write
  • 不保留原形链

参数transfer Transferable objects

transferable object
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容