js对象深拷贝

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
<script type="text/javascript">
    var obj = {
        a: { b: 10 }
    }

    function copy(obj){
        if(typeof obj != 'object'){
            return obj;
        }
        var newObj = {};
        for(attr in obj){
            newObj[attr] = copy(obj[attr]);
        }
        return newObj;
    }

    var obj2 = copy(obj);
    obj2.a.b =20;
    alert(obj.a.b);//10
</script>
</head>
<body>
    
</body>
</html>

利用递归对复杂对象进行拷贝

原文地址:https://www.cnblogs.com/toodeep/p/4474060.html