lenorewei
12/12/2017 - 11:22 AM

深拷贝

function isDef(value) {
    return value !== undefined && value !== null;
}
const { hasOwnProperty } = Object.prototype;

function isObj(x) {
    const type = typeof x;
    return x !== null && (type === 'object' || type === 'function');
};

function assignKey(to, from, key) {
    const val = from[key];

    if (!isDef(val) || (hasOwnProperty.call(to, key) && !isDef(to[key]))) {
        return;
    }

    if (!hasOwnProperty.call(to, key) || !isObj(val)) {
        to[key] = val;
    } else {
        to[key] = assign(Object(to[key]), from[key]);
    }
};

function assign(to, from) {
    for (const key in from) {
        if (hasOwnProperty.call(from, key)) {
            assignKey(to, from, key);
        }
    }
    return to;
};

function deepClone(obj) {
    if (Array.isArray(obj)) {
        return obj.map(item => deepClone(item));
    } else if (typeof obj === 'object') {
        return deepAssign({}, obj);
    }
    return obj;
};