js深拷贝

2020-11-24  本文已影响0人  liuyeqing

function deepClone(obj = {}) {

    if (typeof obj !== 'object' || typeof obj == null) {

        return obj;

    }

    let result;

    if (obj instanceof Array) {

        result = [];

    } else {

        result = {};

    }

    for (let key in obj) {

        if (obj.hasOwnProperty(key)) {

            result[key] = deepClone(obj[key]);

        }

    }

    return result

}

上一篇 下一篇

猜你喜欢

热点阅读