参考网址: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. CSRF攻击原理及防御

    一.CSRF攻击原理 CSRF是什么呢?CSRF全名是Cross-site request forgery,是一种对网站的恶意利用,CSRF比XSS更具危险性.想要深入理解CSRF的攻击特性我们有必要 ...

  2. Spring Cloud中的负载均衡策略

    在上篇博客(Spring Cloud中负载均衡器概览)中,我们大致的了解了一下Spring Cloud中有哪些负载均衡器,但是对于负载均衡策略我们并没有去详细了解,我们只是知道在BaseLoadBal ...

  3. Spring中bean实例化的三种方式

    之前我已经有好几篇博客介绍Spring框架了,不过当时我们都是使用注解来完成注入的,具体小伙伴可以参考这几篇博客(Spring&SpringMVC框架案例).那么今天我想来说说如何通过xml配 ...

  4. Java基础17:Java IO流总结

    更多内容请关注微信公众号[Java技术江湖] 这是一位阿里 Java 工程师的技术小站,作者黄小斜,专注 Java 相关技术:SSM.SpringBoot.MySQL.分布式.中间件.集群.Linux ...

  5. 前端笔记之HTML5&CSS3(中)选择器&伪类伪元素&CSS3效果&渐变背景&过渡

    一.CSS3选择器 CSS3是CSS的第三代版本,新增了很多功能,例如:强大的选择器.盒模型.圆角.渐变.动画.2D/3D转换.文字特效等. CSS3和HTML5没有任何关系!HTML5骨架中,可以用 ...

  6. LEMP平台全编译搭建

    1.安装nginx1.13 1.1解决依赖关系 编译安装nginx需要事先需要安装开发包组"Development Tools"和 "Development Librar ...

  7. SpringMVC集成rabbitmq:优化秒杀下单环节

    前言 上一篇在springboot中基于自动配置集成了rabbitmq.那么回到最初的话题中就是想在秒杀下单环节增加排队机制,从而达到限流的目的. 优化秒杀下单流程 之前是在控制器里拿到客户端请求后直 ...

  8. 练习使用 __attribute__ 属性(仿照内核)

    上一篇文章分析了内核中各种 initcall 的调用过程,在这个基础上大概掌握了内核中使用的这种方法,于是参考内核及网友的文章自己动手写了下,记录在这个随笔中. 源代码如下: #include < ...

  9. Java开发笔记(六十三)双冒号标记的方法引用

    前面介绍了如何自己定义函数式接口,本文接续函数式接口的实现原理,阐述它在数组处理中的实际应用.数组工具Arrays提供了sort方法用于数组元素排序,可是并未提供更丰富的数组加工操作,比如从某个字符串 ...

  10. vue 常用语法糖

    //来自 https://www.cnblogs.com/lhl66/p/8021730.html 侵删 el:element 需要获取的元素,一定是HTML中的根容器元素 data:用于数据的存储 ...