js如何深度克隆

var json = {a:6,b:4,c:[1,2,3]};
var json2 = clone(json);

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;
}
posted @ 2019-02-27 18:01  FE-神鸟  阅读(208)  评论(0编辑  收藏  举报