深拷贝copy

function deepClone(obj){
        var cloneObj=Array.isArray(obj)?[]:{}
        for(key in obj){
            if(typeof obj[key]=="object"){
                cloneObj[key]=deepClone(obj[key])
            }else{
                cloneObj[key]=obj[key]
            }
        }
        return cloneObj
    }
    var obj={
        a:1,
        b:{b:2,c:3},
        c:[1,2,3]
    }
    console.log(deepClone(obj))

你可能感兴趣的:(javascript)