Jquery extend function

本文介绍了jQuery.extend方法的使用,包括如何合并多个对象到一个目标对象中,并解释了后期元素覆盖前期元素的概念。此外还介绍了如何利用该方法扩展jQuery对象及原型,并提供了深拷贝和浅拷贝的实例。

jQuery.extend function introduction

JQuery.extend Extension method:
    Extension method of jQuery extend is usually method when write plugin,
      一、jquery prototype method   

extend(dest,src1,src2,src3...);


      it mean merge src1 src2 src3 into dest,result is dest merged,It can be seen that after according to methods ,the dest already modify。if you want't get  modified dest,you can use :

var newSrc=$.extend({},src1,src2,src3...)


      So you can merge SRC1, src2, src3...。

for example:

var result=$.extend({},{name:"Tom",age:21},{name:"Jerry",sex:"Boy"})

 

      and result is 

result={name:"Jerry",age:21,sex:"Boy"}


      It meas the later element will cover front element

      二、omit dest
      for example:
   1、$.extend(src)
   the way can merge the attribution into jquery object:

$.extend({
hello:function(){alert('hello');}
});


   
   2、$.fn.extend(src)
    the way can merge the attribution into jquery prototype:

$.fn.extend({
hello:function(){alert('hello');}
});

 

   

   list some example under:

$.extend({net:{}});

 

   this is way that can extent net space in jquery global objec

$.extend($.net,{
hello:function(){alert('hello');}
})


    

   三、Jquery.extend方法also have one prototype:  

extend(boolean,dest,src1,src2,src3...)


      first params boolean is represent whether if deep copy,for example:

var result=$.extend( true, {}, 
{ name: "John", location: {city: "Boston",county:"USA"} }, 
{ last: "Resig", location: {state: "MA",county:"China"} } );


      we can seen object of src1 location:{city:"Boston"},object of src2 location:{state:"MA"},merge result is:: 

result={name:"John",last:"Resig",
location:{city:"Boston",state:"MA",county:"China"}}

 

       so it will merge nested object in src,if first boolean is false,we look ,for example:

var result=$.extend( false, {}, 
{ name: "John", location:{city: "Boston",county:"USA"} }, 
{ last: "Resig", location: {state: "MA",county:"China"} } 
);


    result is:

result={name:"John",last:"Resig",location:{state:"MA",county:"China"}}

 

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值