美文网首页
js深拷贝

js深拷贝

作者: liuyeqing | 来源:发表于2020-11-24 00:05 被阅读0次

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

}

相关文章

网友评论

      本文标题:js深拷贝

      本文链接:https://www.haomeiwen.com/subject/ayxviktx.html