javascript深度克隆

seeklanguage發表於2019-03-27
//深度克隆
function deepClone(obj) {
    var oType = getObjectType(obj);
    if (oType == "Object") {
        var result = {}
        for (key in obj) {
            result[key] = deepClone(obj[key]);
        }
        return result;
    } else if (oType == "Array") {
        var result = [];
        for (var i = 0; i < obj.length; i++) {
            result[i] = deepClone(obj[i]);
        }
        return result;
    } else {
        return obj;
    }
}

function getObjectType(o) {
    if (o === null) return "Null";
    if (o === undefined) return "Undefined";
    return Object.prototype.toString.call(o).slice(8, -1);
}

相關文章