This article mainly introduces how to use extend in JQuery. Friends who need it can refer to
extend in Jquery. The prototype of the extension method is:
1. extend(dest,src1,src2,src3...);
It means to merge src1, src2, src3... into dest and return the value It is the merged dest. It can be seen that this method modifies the structure of dest after merging. If you want to get the merged result but don’t want to modify the structure of dest, you can use it as follows:
2, var newSrc=$.extend({},src1,src2,src3. ..)//That is, use "{}" as the dest parameter.
In this way, src1, src2, src3... can be merged, and then the merged result will be returned to newSrc.
The following example:
The code is as follows:
var result=$.extend({},{name:"Tom",age:21},{name:" Jerry",sex:"Boy"})
Then the merged result
result={name:"Jerry",age:21,sex:"Boy"}
In other words, what follows If the parameter has the same name as the previous parameter, the later parameter will overwrite the previous parameter value.
3. extend(boolean,dest,src1,src2,src3...)
The first parameter boolean represents whether to perform a deep copy, and the other parameters are the same as previously introduced
For example
The code is as follows:
var result=$.extend( true, {},
{ name: "John", location: {city: "Boston", county:"USA"} },
{ last: "Resig", location: {state: "MA",county:"China"} } );
We can see that the sub-object is nested in src1 location:{city:"Boston"}, and the sub-object location:{state:"MA"} is also nested in src2, the first deep copy If the parameter is true, then the merged result is:
{ name: "John", location:{city: "Boston",county:"USA"} },
{ last: "Resig", location: {state: "MA",county:"China"} } );
The above is the detailed content of Analyze the usage details of extend in JQuery. For more information, please follow other related articles on the PHP Chinese website!