Introduction to extend in JQuery

  • 2020-03-30 02:24:05
  • OfStack

The prototype extension method of extend in Jquery is:

1, the extend (dest, src1, src2, src3...). ;
It means that src1,src2,src3... Merge into dest and return the value as the merged dest, so you can see that this method has modified the structure of dest after merging. If you want to get the results of the merge but do not want to modify the structure of dest, you can use the following:

2, var newSrc = $. The extend ({}, src1, src2, src3...). // that is, take "{}" as the dest parameter.
This will allow src1,src2,src3... Merge and return the result to newSrc.
The following cases:

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

So the result of the merge
Result = {name: "Jerry," the age: 21, sex: "Boy"}
That is, if the following parameter has the same name as the previous parameter, then the following parameter will override the previous parameter value.

3, the extend (Boolean, dest, src1, src2, src3...).
The first parameter, Boolean, indicates whether or not to make a depth copy, and the remaining parameters are consistent with those described earlier
For example,
 
var result=$.extend( true, {}, 
{ name: "John", location: {city: "Boston",county:"USA"} }, 
{ last: "Resig", location: {state: "MA",county:"China"} } ); 

We can see that the nested subobject location:{city:"Boston"} in src1 is nested, and the nested subobject location:{state:"MA"} in src2 is also nested. The first depth copy parameter is true, so the merged result is:
 
result={name:"John",last:"Resig",location:{city:"Boston",state:"MA",county:"China"}} 

That is to say, it will also merge nested child objects in SRC. If the first parameter Boolean is false, let's see what the merged result is, as follows:
 
var result=$.extend( false, {}, 
{ name: "John", location:{city: "Boston",county:"USA"} }, 
{ last: "Resig", location: {state: "MA",county:"China"} } ); 

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


Related articles: