First, the extension method
notation :extend (dest,src1,src2,src3 ...);
parameter Description : It means 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
1 var newsrc=$.extend ({},src1,src2,src3 ...) // That is, "{}" as the Dest parameter.
Example :
1 var result=$.extend ({},{name: "Tom", Age:21},{name: "Jerry", Sex: "Boy"})
Results are result={Name:"Jerry", Age:21st, Sex:"boy "}
Second, omit the dest parameter
The Dest parameter in the Extend method prototype 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;
$.extend (SRC) The method is to merge src into the global object of jquery, such as:
1 $.extend ({2 Hello:function() {alert (' hello ');} 3 });
is to merge the Hello method into the global object of jquery.
$.fn.extend (SRC) is also the same;
Example :
$.extend ({net:{}}); $.extend ($.net,{ Hello:function() {alert (' Hello ')} )
Or
$.net = $.net | | {};$.extend ($.net,{ Hello:function() {alert (' hello ');} })
This is the extension of the Hello method into jquery's net namespace. This is also the code that is written at the beginning of the jquery UI source.
Three, heavy-duty prototypes
1 Extend (boolean, DEST,SRC1,SRC2,SRC3 ...)
The first parameter, Boolean, indicates whether to make a deep copy, the rest of the parameters are consistent with what was described earlier, what is called deep copy, and we look at an example:
1 var true , {}, 2 {name: "John", Location: {City: "Boston", County: "USA"}}, 3
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:
1 result={name: "John", Last: "Resig",2 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:
1 var false , {}, 2 {name: "John", location:{city: "Boston", County: "USA"}, 3 {last: "Resig", location: { State: ' MA ', County: ' China ' }4
Then the result of the merger is:
1 result={name: "John", Last: "Resig", Location:{state: "MA", County: "China"}
Jquery.extend detailed