深拷贝方法

深拷贝

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拷贝方法html

相关文章
相关标签/搜索