Analysis of the usages of extend in jquery _jquery

Source: Internet
Author: User

This example describes the use of extend in jquery. Share to everyone for your reference. The specific analysis is as follows:

The Extend () function is one of the fundamental functions of jquery, which is to extend an existing object. Extend is a common method in the process of writing plug-ins, which has some overloaded prototypes. $.extend (prop) is used to extend the jquery object and can be used to add functions to the jquery namespace.

First, the Jquery.extend function of the source code

Jquery.extend = JQuery.fn.extend = function () {var options, name, SRC, copy, Copyisarray, clone, target = argument S[0] | |
  {},//parameter target object i = 1, length = arguments.length,//parameter length deep = false;//for deep copy//Handle a deep copy situation
    If a depth copy is made, both the target object and the original object cursor value I and the depth value are updated if (typeof target = = "Boolean") {deep = target; target = Arguments[1] | |
    {};
  Skip the Boolean and the target i = 2; //Handle case when the target is a string or something (possible in deep copy)//If the target object's value type is wrong, reset to {} if (typeof Targ
  ET!== "Object" &&!jquery.isfunction (target) {target = {}; }//Extend jquery itself if only one argument is passed///when the parameter value length is 1, the target object is jquery itself if (length = = i) {ta
    Rget = this;
  I.;  for (; i < length; i++) {//only deal with non-null/undefined values if (options = arguments[i])!= NULL) {//Ignore empty object//Extend the Base object for (name in options) {src = target[Name]; 
        copy = options[name];//Store the value of the object//Prevent never-ending loop if (target = = copy) {continue; }//Recurse If we re merging plain objects or arrays//deep copy structure of object relationships that have more than two layers of attribute depth, such as {a:{s:21,age:1 1}} or {a:[' s ' =>21, ' age ' =>11]} if (Deep && copy && jquery.isplainobject (copy) | | (Copyisarray = Jquery.isarray (copy))
            {if (Copyisarray) {//If it is an array object copyisarray = false; clone = src && jquery.isarray (src)?

          SRC: [];
          else {//normal object clone = src && jquery.isplainobject (src)? src: {}; }//Never Move original objects, clone them//call itself for recursive replication target[name] = Jquery.extend (

        Deep, clone, copy); Don ' t bring in undefined values} else if (copy!== undefined) {//non-depth CP direct overwrite target attribute target[name] = Co
        Py }}//Return the modified objECT return target;

 };

Two, the jquery extension method prototype is:
  
1, Extend (dest,src1,src2,src3 ...);

It means to src1,src2,src3 ... Merged into Dest, the return value is the merged dest, which shows that the method is modified to dest structure after merging. If you want to get the results of a merge but do not want to modify the structure of the dest, you can use the following:

2, Var newsrc=$.extend ({},src1,src2,src3 ...) That is, "{}" as the Dest parameter.

This can be src1,src2,src3 ... To 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 combined results

Result={name: "Jerry", Age:21,sex: "Boy"}

That is, if the following argument has the same name as the previous argument, then the previous parameter value will be overwritten.

3, Extend (BOOLEAN,DEST,SRC1,SRC2,SRC3 ...)

The first argument Boolean represents whether to make a deep copy, and the rest of the parameters are consistent with the previous
For example:

var Result=$.extend (True, {}, 
{name: "John", Location: {City: "Boston", County: "USA"}}, 
{last: "Resig", Locatio N: {state: "MA", County: "" "});

We can see that the nested SRC1 in the location:{city: "Boston"},SRC2 also have nested objects location:{state: "MA"}, the first depth copy parameter is true, then the result of the merge is:

Result={name: "John", Last: "Resig", location:{city: "Boston", State: "MA", County: "The"

That is, it merges the nested subforms in SRC, and if the first argument is false, let's see what the result of the merge looks like:

var result=$.extend (False, {}, 
{name: "John", location:{city: "Boston", County: "USA"}}, 
{last: "Resig", Locati On: {state: "MA", County: "" "});

Then the result of the merger is:

Result={name: "John", Last: "Resig", Location:{state: "MA", County: "Our"}

Ii. the case of omitting dest parameters by extend method in jquery

The Dest parameter in the above extend method prototype can be omitted, and if omitted, the method can have only one src parameter, and the SRC is merged into an object that invokes the Extend method, such as:

1, $.extend (SRC)

The approach is to merge SRC into the global object of jquery, such as:

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

is to incorporate 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 example illustrates 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 to extend the Hello method to the net namespace of the previously extended jquery.

I hope this article will help you with your jquery programming.

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.