深拷贝方法

Posted bonly-ge

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了深拷贝方法相关的知识,希望对你有一定的参考价值。

深拷贝

function deepClone(obj) {
    if(obj == null) return obj; // null == undefined
    if(obj instanceof Date) return new Date(obj);
    if(obj instanceof RegExp) return new RegExp(obj);
    if(typeof obj != ‘object‘) return obj;
    let newObj = new obj.constructor;
    for(let key in obj) {
        newObj[key] = deepClone(obj[key]);
    }
    return newObj;
}
参考地址

JSON.stringify复制对象特点
一步步推导出的深拷贝
jquery中extend拷贝方法
Object.create拷贝方法




以上是关于深拷贝方法的主要内容,如果未能解决你的问题,请参考以下文章