javaScript高级教程(四) 复制对象

//返回新对象,双方互不影响
function clone(obj){   
    //alert('clone');
    if(typeof(obj) != 'object') return obj;
    if(obj == null) return obj; //因为typeof(null) == object所以要加上这步
    var newObj = {};
    for(var i in obj){
        newObj[i] = clone(obj[i]);
        //alert('obj['+i+'] '+obj[i]);
    }
    return newObj;
}


function clone2(obj){
    //alert('clone2');
    function F(){}
    F.prototype = obj;
    return new F();
}
原文地址:https://www.cnblogs.com/yuyutianxia/p/3273448.html