Home > Web Front-end > JS Tutorial > Introduction to the use of extend in JQuery_jquery

Introduction to the use of extend in JQuery_jquery

WBOY
Release: 2016-05-16 16:55:51
Original
839 people have browsed it

The prototype of the extend method in Jquery is:
 
1. extend(dest,src1,src2,src3...);
It means to extend src1,src2,src3... Merged into dest, the return value 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 dest structure, 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:

Copy code 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"}
That is to say, if the later parameter has the same name as the previous parameter, then 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
Copy code 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 location: {city: "Boston"} is nested in src1, and the sub-object location: {state: "MA"} is also nested in src2. If a deep copy parameter is true, then the merged result is:
Copy code The code is as follows:

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

Also That is to say, it will also merge the nested sub-objects in src. If the first parameter boolean is false, let’s see what the result of the merger is, as follows:
Copy code 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:
Copy code The code is as follows:

result={name :"John",last:"Resig",location:{state:"MA",county:"China"}}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template