深拷貝方法

深拷貝

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

相關文章
相關標籤/搜索