合作联系微信: w6668263      合作联系电话:177-9238-7426     

javascript深度克隆对象

/**
 *
 * @param obj
 * @returns {*}
 */
//深度克隆
function cloneObject(obj) {

    if (obj === null || typeof(obj) !== 'object' || 'isActiveClone' in obj)
        return obj;

    if (obj instanceof Date)
        var temp = new obj.constructor(); //or new Date(obj);
    else
        var temp = obj.constructor();

    for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
            obj['isActiveClone'] = null;
            temp[key] = cloneObject(obj[key]);
            delete obj['isActiveClone'];
        }
    }

    return temp;
}

技术交流QQ群:15129679

posted on 2016-11-26 16:04  草率的龙果果  阅读(230)  评论(0编辑  收藏  举报

导航