怎么在jQuery中使用$.extend深拷贝对象($.extend,jquery,编程语言)

时间:2024-04-29 13:32:14 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

语法:jQuery.extend( [deep ], target, object1 [, objectN ] )

深浅拷贝对应的参数就是[deep],是可选的,为true或false。默认情况是false(浅拷贝),并且false是不能够显示的写出来的。如果想写,只能写true(深拷贝)

测试例子:

varobject1={};varobject2={b:{mm:333},c:100};console.log('原来的object1--->'+JSON.stringify(object1));console.log('原来的object2--->'+JSON.stringify(object2));$.extend(true,object1,object2);object1.b.mm=600;console.log('新的object1--->'+JSON.stringify(object1));console.log('新的object2--->'+JSON.stringify(object2));

测试结果

怎么在jQuery中使用$.extend深拷贝对象

由测试结果知道,jQuery中$.extend(true,object1, object2);可以深拷贝对象,拷贝之后,改变其中一个对象的属性值,对另外一个没有影响。

接着在继续深入理解

测试例子

varobject1={a:0,b:{gg:11,mm:22}};varobject2={b:{mm:333},c:100};//默认情况浅拷贝//object1--->{"apple":0,"banana":{"price":200},"cherry":97,"durian":100}//object2的banner覆盖了object1的banner,但是weight属性未被继承//$.extend(object1,object2);//深拷贝//object1--->{"apple":0,"banana":{"weight":52,"price":200},"cherry":97,"durian":100}//object2的banner覆盖了object1的banner,但是weight属性也被继承了呦$.extend(true,object1,object2);console.log('原来的object1--->'+JSON.stringify(object1));console.log('原来的object2--->'+JSON.stringify(object2));object1.b.mm=600;console.log('新的object1--->'+JSON.stringify(object1));console.log('新的object2--->'+JSON.stringify(object2));

测试结果

怎么在jQuery中使用$.extend深拷贝对象

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:怎么在jQuery中使用$.extend深拷贝对象的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:PHP如何实现for循环下一篇:

4 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18