参考网址:http://bijian1013.iteye.com/blog/2255037
jQuery.extend() 函数用于将一个或多个对象的内容合并到目标对象。
注意:1. 如果只为$.extend()指定了一个参数,则意味着参数target被省略。此时,target就是jQuery对象本身。通过这种方式,我们可以为全局对象jQuery添加新的函数。
2. 如果多个对象具有相同的属性,则后者会覆盖前者的属性值。
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>$.extend()浅拷贝深拷贝</title> 6 <script src="http://apps.bdimg.com/libs/jquery/1.8.1/jquery.min.js"></script> 7 </head> 8 <body> 9 <!--参考网址:http://bijian1013.iteye.com/blog/2255037--> 10 <script type="text/javascript"> 11 $(function () { 12 var list = function () { 13 this.a = { 14 name : 'zhangshan', 15 age : 28, 16 company : { 17 name : '腾讯', 18 address : '深圳', 19 size : 10000 20 } 21 }; 22 this.b={ 23 name : 'lisi', 24 age : 30, 25 company : { 26 name : '阿里巴巴', 27 address : '杭州' 28 }, 29 stature : '172cm' 30 }; 31 this.show = function (c) { 32 console.log('a:' + JSON.stringify(this.a)); 33 console.log('b:' + JSON.stringify(this.b)); 34 console.log('c:' + JSON.stringify(c)); 35 console.log(' ') 36 } 37 }; 38 /*浅拷贝*/ 39 q1(); 40 q2(); 41 q3(); 42 /*深拷贝*/ 43 s1(); 44 s2(); 45 s3(); 46 s4(); 47 function q1() { 48 var q1 = new list; 49 var c = $.extend(q1.a,q1.b); 50 q1.show(c); 51 // a:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"} 52 // b:{"name":"test","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"} 53 // c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"} 54 }; 55 function q2() { 56 var q2 = new list; 57 var c = $.extend(q2.a,q2.b); 58 q2.b.name = 'test'; 59 q2.b.company.address = '北京'; 60 q2.show(c); 61 // a:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"} 62 // b:{"name":"test","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"} 63 // c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"} 64 }; 65 function q3() { 66 var q3 = new list; 67 var c = $.extend({},q3.a,q3.b); 68 q3.show(c); 69 // a:{"name":"zhangshan","age":28,"company":{"name":"腾讯","address":"深圳","size":10000}} 70 // b:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州"},"stature":"172cm"} 71 // c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州"},"stature":"172cm"} 72 }; 73 function s1() { 74 var s1 = new list; 75 var c = $.extend(true,s1.a,s1.b); 76 s1.show(c); 77 // a:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州","size":10000},"stature":"172cm"} 78 // b:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州"},"stature":"172cm"} 79 // c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州","size":10000},"stature":"172cm"} 80 }; 81 function s2() { 82 var s2 = new list; 83 var c = $.extend(true,s2.a,s2.b); 84 s2.b.name = 'test'; 85 s2.b.company.address = '北京'; 86 s2.show(c); 87 // a:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州","size":10000},"stature":"172cm"} 88 // b:{"name":"test","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"} 89 // c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州","size":10000},"stature":"172cm"} 90 }; 91 function s3() { 92 var s3 = new list; 93 var c = $.extend(true,{},s3.a,s3.b); 94 s3.show(c); 95 // a:{"name":"zhangshan","age":28,"company":{"name":"腾讯","address":"深圳","size":10000}} 96 // b:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州"},"stature":"172cm"} 97 // c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州","size":10000},"stature":"172cm"} 98 }; 99 function s4() { 100 var s4 = new list; 101 var c = $.extend(true,{},s4.a,s4.b); 102 s4.a.name = 'zhangshan01'; 103 s4.b.name = 'lisi01'; 104 s4.b.company.address = 'shengzhen'; 105 s4.show(c); 106 // a:{"name":"zhangshan01","age":28,"company":{"name":"腾讯","address":"深圳","size":10000}} 107 // b:{"name":"lisi01","age":30,"company":{"name":"阿里巴巴","address":"shengzhen"},"stature":"172cm"} 108 // c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州","size":10000},"stature":"172cm"} 109 }; 110 }) 111 </script> 112 </body> 113 </html>