码迷,mamicode.com
首页 > Web开发 > 详细

angular.js 的angular.copy 、 angular.extend 、 angular.merge

时间:2016-11-09 17:41:12      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:xtend   head   merge   objects   color   ons   htm   javascrip   title   

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <script src="js/angular.min.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript">
            //angular.copy is deep copy
            var o1 = {
                name: xudongyang,
                age: 26,
                skill:{}
            }
            var o2 = angular.copy(o1);
            console.log(o2);
            console.log(o1 == o2);
            
            //extend is shallow copy of the properties of the source objects from right to left 
            var src1 = {
                name: xudongyang,
                age: 20
            }
            var src2 = {
                age: 10,
                skill: {}
            }
            var dst = {}, 
                dst2 = angular.extend(dst , src2 , src1)
            console.log(dst2.skill == dst.skill);
            src2.skill.eat = "kaorou";
            console.log(dst2.skill);
            console.log(dst.skill);
            
            //angular.merge is deep copy
            var m1 = {name:xudongyang , age:29},
                m2 = {age:10 , skill:{}}
                dstm = {};
                dstm2 = angular.merge(dstm , m2 , m1);
            console.log(dstm2);
            console.log(m2.skill == dstm2.skill);
            
            //angular.extend vs angular.merge
            var s1 = {skill:{name:java , experience: 20, certified: true}},
                s2 = {skill:{name:js , experience: 10}},
                dst = {},
                dst2 = angular.extend(dst , s1 , s2);
                console.log(dst);
                
            //angular.extend vs angular.merge
            var s1 = {skill:{name:java , experience: 20, certified: true}},
                s2 = {skill:{name:js , experience: 10}},
                dst = {},
                dst2 = angular.merge(dst , s1 , s2);
                console.log(dst);
            //浅克隆只克隆一层,深克隆一直复制到最底层
        </script>
    </body>
</html>

 

angular.js 的angular.copy 、 angular.extend 、 angular.merge

标签:xtend   head   merge   objects   color   ons   htm   javascrip   title   

原文地址:http://www.cnblogs.com/xudy/p/6047796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!