深克隆

/**
 * 深克隆
 */
function deepClone(obj) {
    if(obj === null) return null;
    if(typeof obj !== 'object') return obj;

    var newObj = obj instanceof Array ? [] : {};
    newObj.constructor = obj.constructor;

    for(var i in obj) {
        if(obj.hasOwnPropety(i)){
            newObj[i] = typeof obj[i] === 'object' ? deepClone(obj[i]) : obj[i];
        }
    }

    return newObj;
}
posted @ 2021-02-23 11:44  hwjun  阅读(60)  评论(0编辑  收藏  举报