js深复制与浅复制

js深复制

var arr = [1,2,43];
var json = {a:6,b:4,c:[1,2,3]};
var str = 'sdfsdf';

var json2 = clone(json);

alert(json['c'])
function clone(obj){
    var oNew = new obj.constructor(obj.valueOf());
    if(obj.constructor == Object){
        for(var i in obj){
            oNew[i] = obj[i];
            if(typeof(oNew[i]) == 'object'){
                    clone(oNew[i]);
            }
        }
    }
    return oNew;
}

标签: none

添加新评论