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();
}

 

posted @ 2013-08-21 21:14  等风来。。  Views(181)  Comments(0Edit  收藏  举报
------------------------------------------------------------------------------------------------------------ --------------- 欢迎联系 x.guan.ling@gmail.com--------------- ------------------------------------------------------------------------------------------------------------