Home  >  Article  >  Web Front-end  >  Analyze the usage details of extend in JQuery

Analyze the usage details of extend in JQuery

巴扎黑
巴扎黑Original
2017-07-09 16:44:10954browse

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:

The code is as follows:

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

That is to say, it will also merge the nested sub-objects in src, and if the first parameter boolean is false, we Take a look at the result of the merger, as follows:


The code is as follows:

var result=$.extend( false, {},

{ name: "John", location:{city: "Boston",county:"USA"} },
{ last: "Resig", location: {state: "MA",county:"China"} } );

Then the merged result is:


The code is as follows:

result={name:"John",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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn