首页 > Web开发 > 详细

js对象深拷贝

时间:2015-05-03 15:56:48      阅读:229      评论:0      收藏:0      [点我收藏+]
<!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>

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

js对象深拷贝

原文:http://www.cnblogs.com/toodeep/p/4474060.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!