Jquery.extend function Explanation

Source: Internet
Author: User

jquery's Extend extension method:

Jquery的扩展方法extend是我们在写插件的过程中常用的方法,该方法有一些重载原型,在此,我们一起去了解了解。

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"})

Then the result of the merger

  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:
 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, $.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:

$.extend ({net:{}});

This is the extension of a net namespace in the jquery global object.

  $.extend ($.net,{   Hello:function() {alert (' hello ');}  })

This is the extension of the Hello method into the net namespace of the previously extended jquery.

third, the Extend method of jquery also has an overloaded prototype:
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:

var true ,  {},      "John", Location: {City: "Boston", County: "USA"},      "Resig", Location: {state: "MA", County: "China"});

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 "}}< c11/>

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.

Add:
We also often use the extend () method to write jquery plugins, so the types of jquery plugins are as follows:

Types of jquery Plugins

1. Package Object Method
This plug-in is the most common plug-in that encapsulates object methods and is used to manipulate jquery objects obtained through selectors. Such plug-ins can take advantage of the jquery selector, and a significant portion of jquery's approach is to "interpolate" the kernel, such as parent() methods, methods, and so on, within the jquery scripting library appendTo() .

2. Encapsulating global Functions
You can add separate functions to the jquery namespace. such as the commonly used Jquery.ajax () method, the Jquery.trim () method of the end-to-end space, is the jquery inside as a global function plug-in attached to the kernel up.

3. Selector plug-in
Although the jquery selector is very powerful, in a few cases, you will need to use a selector plug-in to augment some of your favorite selectors.

JQuery.fn.extend () is used to extend the first of the 3 types mentioned above, and Jquery.extend () is used to extend the latter two plugins. Both methods accept a parameter, which is of type object. The "name/value pairs" of the object objects represent "function or method name/function Body", respectively.

Reprint Address: http://www.cnblogs.com/RascallySnake/archive/2010/05/07/1729563.html

Jquery.extend function Explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.