Jquery extend function

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"}}

 

  

猜你喜欢

转载自blog.csdn.net/sunrunning/article/details/80199941