$.extend()浅拷贝深拷贝

参考网址:http://bijian1013.iteye.com/blog/2255037

jQuery.extend() 函数用于将一个或多个对象的内容合并到目标对象。


注意:1. 如果只为$.extend()指定了一个参数,则意味着参数target被省略。此时,target就是jQuery对象本身。通过这种方式,我们可以为全局对象jQuery添加新的函数。
2. 如果多个对象具有相同的属性,则后者会覆盖前者的属性值。

 
 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$.extend()浅拷贝深拷贝</title>
<script src="http://apps.bdimg.com/libs/jquery/1.8.1/jquery.min.js"></script>
</head>
<body>
<!--参考网址:http://bijian1013.iteye.com/blog/2255037-->
<script type="text/javascript">
$(function () {
var list = function () {
this.a = {
name : 'zhangshan',
age : 28,
company : {
name : '腾讯',
address : '深圳',
size : 10000
}
};
this.b={
name : 'lisi',
age : 30,
company : {
name : '阿里巴巴',
address : '杭州'
},
stature : '172cm'
};
this.show = function (c) {
console.log('a:' + JSON.stringify(this.a));
console.log('b:' + JSON.stringify(this.b));
console.log('c:' + JSON.stringify(c));
console.log('\n')
}
};
/*浅拷贝*/
q1();
q2();
q3();
/*深拷贝*/
s1();
s2();
s3();
s4();
function q1() {
var q1 = new list;
var c = $.extend(q1.a,q1.b);
q1.show(c);
// a:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"}
// b:{"name":"test","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"}
// c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"}
};
function q2() {
var q2 = new list;
var c = $.extend(q2.a,q2.b);
q2.b.name = 'test';
q2.b.company.address = '北京';
q2.show(c);
// a:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"}
// b:{"name":"test","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"}
// c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"}
};
function q3() {
var q3 = new list;
var c = $.extend({},q3.a,q3.b);
q3.show(c);
// a:{"name":"zhangshan","age":28,"company":{"name":"腾讯","address":"深圳","size":10000}}
// b:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州"},"stature":"172cm"}
// c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州"},"stature":"172cm"}
};
function s1() {
var s1 = new list;
var c = $.extend(true,s1.a,s1.b);
s1.show(c);
// a:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州","size":10000},"stature":"172cm"}
// b:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州"},"stature":"172cm"}
// c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州","size":10000},"stature":"172cm"}
};
function s2() {
var s2 = new list;
var c = $.extend(true,s2.a,s2.b);
s2.b.name = 'test';
s2.b.company.address = '北京';
s2.show(c);
// a:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州","size":10000},"stature":"172cm"}
// b:{"name":"test","age":30,"company":{"name":"阿里巴巴","address":"北京"},"stature":"172cm"}
// c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州","size":10000},"stature":"172cm"}
};
function s3() {
var s3 = new list;
var c = $.extend(true,{},s3.a,s3.b);
s3.show(c);
// a:{"name":"zhangshan","age":28,"company":{"name":"腾讯","address":"深圳","size":10000}}
// b:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州"},"stature":"172cm"}
// c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州","size":10000},"stature":"172cm"}
};
function s4() {
var s4 = new list;
var c = $.extend(true,{},s4.a,s4.b);
s4.a.name = 'zhangshan01';
s4.b.name = 'lisi01';
s4.b.company.address = 'shengzhen';
s4.show(c);
// a:{"name":"zhangshan01","age":28,"company":{"name":"腾讯","address":"深圳","size":10000}}
// b:{"name":"lisi01","age":30,"company":{"name":"阿里巴巴","address":"shengzhen"},"stature":"172cm"}
// c:{"name":"lisi","age":30,"company":{"name":"阿里巴巴","address":"杭州","size":10000},"stature":"172cm"}
};
})
</script>
</body>
</html>

随机推荐

  1. Sqli-labs less 26a

    Less-26a 这关与26的区别在于,sql语句添加了一个括号,同时在sql语句执行抛出错误后并不在前台页面输出.所有我们排除报错注入,这里依旧是利用union注入. sql语句为SELECT * ...

  2. 搬瓦工vps搭建vpn

    一.下载centos6一键安装包 wget --no-check-certificate https://raw.githubusercontent.com/teddysun/across/maste ...

  3. HDOJ&lpar;HDU&rpar; 2123 An easy problem&lpar;简单题&period;&period;&period;&rpar;

    Problem Description In this problem you need to make a multiply table of N * N ,just like the sample ...

  4. &lbrack;luogu P2184&rsqb; 贪婪大陆 &lbrack;树状数组&rsqb;&lbrack;线段树&rsqb;

    题目背景 面对蚂蚁们的疯狂进攻,小FF的Tower defence宣告失败……人类被蚂蚁们逼到了Greed Island上的一个海湾.现在,小FF的后方是一望无际的大海, 前方是变异了的超级蚂蚁. 小 ...

  5. shell解析命令行的过程以及eval命令

    本文说明的是一条linux命令在执行时大致要经过哪些过程?以及这些过程的大致顺序. 1.1 shell解析命令行 shell读取和执行命令时的大致操作过程如下图: 以执行以下命令为例: echo -e ...

  6. ubuntu重启、关机命令

    重启命令 :     1.reboot     2.shutdown -r now 立刻重启    3.shutdown -r 10 过10分钟自动重启    4.shutdown -r 20:35 ...

  7. 一些常用的js循环,如for

    https://blog.csdn.net/u014399368/article/details/82862444

  8. WCF三种通信方式

    一.概述 WCF在通信过程中有三种模式:请求与答复.单向.双工通信.以下我们一一介绍. 二.请求与答复模式 描述: 客户端发送请求,然后一直等待服务端的响应(异步调用除外),期间处于假死状态,直到服务 ...

  9. uva-10245-分治

    题意:数组二维空间内的点,求最近的俩个点的距离. 根据x排序,求左部分的最近距离,右部分最近距离,然后以中点,当前距离为半径,计算所有的点距离. #include <string> #in ...

  10. &lowbar;&lowbar;slots&lowbar;&lowbar;,&lowbar;&lowbar;doc&lowbar;&lowbar;&comma;&lowbar;&lowbar;del&lowbar;&lowbar;&comma;&lowbar;&lowbar;call&lowbar;&lowbar;,&lowbar;&lowbar;iter&lowbar;&lowbar;&comma;&lowbar;&lowbar;next&lowbar;&lowbar;迭代器协议(三十六)

    1.__slots__是什么:是一个类变量,变量值可以是列表,元祖,或者可迭代对象,也可以是一个字符串(意味着所有实例只有一个数据属性) 2.引子:使用点来访问属性本质就是在访问类或者对象的__dic ...

上一篇:机器学习技术(2)---K-Means聚类算法


下一篇:Windows平台交叉编译Arm Linux平台的QT5.7库