对象深拷贝

2022-01-06  本文已影响0人  INGME
function  deeClone(origin, target) {
    var target = target || {},
        toStr = Object.prototype.toString,
        arrType = '[object Array]';

    for(var key in origin) {
        if(origin.hasOwnProperty(key)) {
            if(typeof(origin[key]) === 'object' && origin[key] !== null) {
                target[key] = toStr.call(origin[key]) === arrType ? [] : {};
                deepClone(origin[key], target[key]);
            } else {
                target[key] = origin[key];
            }
        }
    }
    return target;
}
上一篇下一篇

猜你喜欢

热点阅读