Transferred from: http://www.cnblogs.com/RascallySnake/archive/2010/05/07/1729563.html
jquery's Extend extension method:
The extension method of jquery extend is a common method in the process of writing plug-ins, there are some overloaded prototypes, and here we go to understand.
One, the extension method of jquery prototype is:
Extend (dest,src1,src2,src3 ...);
Its meaning is to be src1,src2,src3 ... Merge into Dest, the return value is the merged dest, so you can see that the method is merged, the structure of the dest is modified.
If you want to get the results of the merge without modifying the structure of the dest, you can use the following:
var newsrc=$.extend ({},src1,src2,src3 ...) // That is, "{}" as the Dest parameter.
This will allow the src1,src2,src3 ... Merge, and then return the merge results to NEWSRC. The following example:
var result=$.extend ({},{name: "Tom", Age:21},{name: "Jerry", Sex: "Boy"})
Results after merging:
Result={name: "Jerry", Age:21,sex: "Boy"}
That is, if the following parameter has the same name as the previous parameter, then the previous parameter value is overwritten.
Second, omit the dest parameter
The Dest parameter in the Extend method prototype above can be omitted, if omitted, then the method can have only one src parameter, and it is to merge the src into the object calling the Extend method, such as:
2.1, $.extend (SRC)
The method is to merge src into the global object of jquery, such as:
$.extend ({ Hello:function() {alert (' hello ');} });
is to merge the Hello method into the global object of jquery.
2.2, $.fn.extend (SRC)
This method merges src into the instance object of jquery, such as:
$.fn.extend ({hello:function() {alert (' hello ');}});
is to merge the Hello method into the instance object of jquery.
The following examples illustrate several common extension instances:
To extend a net namespace in the jquery global object:
$.extend ({net:{}});
Extend the Hello method to the net namespace of the previously extended jquery:
$.extend ($.net,{ Hello:function() {alert (' hello ');}})
third, the Extend method of jquery also has an overloaded prototype:
Extend (boolean, DEST,SRC1,SRC2,SRC3 ...)
3.1 The first parameter, Boolean, indicates whether deep copies are made, and the remaining parameters are consistent with those described earlier.
What do you mean deep copy? Let's look at an example:
var true , {} , "John", Location: {City: "Boston", County: "USA"},
We can see that the nested SRC1 in the location:{city: "Boston"}, Src2 also nested the object location:{state: "MA"}, the first deep copy parameter is true, then the result of the merge is:
result={
Name: "John",
Last: ' Resig ', location:{city:' Boston ', State: ' MA ', County: ' China '}
};
That is, it merges the nested objects in SRC as well, and if the first argument is a Boolean false, let's see what the result of the merge is, as follows:
var false , {}, " John ", location:{city:" Boston ", County:" USA "}, " Resig ", Location: {state: ' MA ', County: ' China '}}
Then the result of the merger is:
result={
Name: "John",
Last: "Resig",
Location:{state: "MA", County: "China"}
}
These are some of the details that $.extend () will often use in the project.
Jquery.extend function Explanation