$.extend,$.fn.extend and (function ($) {...}) (JQuery);

Source: Internet
Author: User

1.$.extend

$.extend (SRC)
The method is to merge src into the global object of jquery.

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

is to merge the Hello method into the global object of jquery.

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

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

2.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 Result=$.extend (True, {},
{Name: "John", Location: {City: "Boston", County: "USA"}},
{Last: ' 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 result=$.extend (False, {},
{Name: "John", location:{city: "Boston", County: "USA"}},
{Last: ' Resig ', location: {state: ' MA ', County: ' China '}}
);

Then the result of the merger is:

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

3.$.fn.extend

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

is to merge the Hello method into the instance object of jquery.

jquery is a well-encapsulated class, such as we use the statement $ ("#btn1") to generate an instance of the jquery class.

JQuery.fn.extend (object); The extension to Jquery.prototype is to add "member functions" to the jquery class. An instance of the jquery class can use this "member function".

4. (function ($) {...}) (JQuery);

Let's first look at the contents of the first parenthesis: function ($) {...}, this is an anonymous function, the formal parameter is $, here is mainly to not conflict with other libraries.

This makes it easier to understand that the first parenthesis is defined as an anonymous function, and we call the function with parentheses and arguments behind the function name, but because the operator's precedence we define the anonymous function to be enclosed.

Now I think we have a good idea of what this sentence means. The first parenthesis indicates that an anonymous function is defined, and then the second function represents the argument passed by the function, and the entire combination means that an anonymous function is defined and then called, and the function's argument is jquery.

Equivalent: function Fun ($) {...}; Fun (jQuery);

This approach is useful for developing plug-ins.

$.extend,$.fn.extend and (function ($) {...}) (JQuery);

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.