JQuery. extend (), jQuery. fn. extend ()

Source: Internet
Author: User

JQuery customizes jQuery. extend () and jQuery. fn. extend () method. jQuery. the extend () method can create global functions or selectors, whereas jQuery. fn. the extend () method can create jQuery object methods.
For example:

JQuery. extend ({showName: function (name) {alert (name) }}); jQuery. showName (" ");

In addition to creating plug-ins, jQuery. extend () can also be used to expand jQuery objects.
For example:

var a = {    name : "blue",    pass : 123}var b = {    name : "red",    pass : 456,    age : 1}var c = jQuery.extend({},a,b);

C has the attributes of object a and object B. Because object B is after object a, its name attribute takes precedence over object c.

The jQuery. extend () method transmits series options for the plug-in, including default values.

Function fn (options) {var options = jQuery. extend ({// default parameter Option List name1: value1, name2: value2, name3: value3}, options ); // use the function parameters to overwrite or merge them to the default parameter options list. // function body} fn ({name1: value3, name2: value2, name3: value1 }); // use the new value fn ({name4: value3, name5: value2}); // Add the new option fn () by default; // retain the default option value

When this method is called, a new parameter value will overwrite the default parameter option value. Otherwise, the default parameter value will be used.


How to Create a JQuery object using a JQuery. fn object

You can use the jQuery. fn object to add attributes and methods. In fact, the jQuery. fn object is attached to jQuery. prototype, which is abbreviated by jQuery.

What is fn. It is not difficult to see jQuery code.


JQuery. fn = jQuery. prototype = {

Init: function (selector, context ){//....

//......

};

It turns out that jQuery. fn = jQuery. prototype. It is certainly no stranger to prototype.


For example:
JQuery. fn. test = function () {alert ("this is the jQuery object method! ");} JQuery (" div "). click (function () {$ (this ). test (); // call the test () method on the current jQuery object });


We can call the jQuery. fn. extend () method to create the jQuery object method.

JQuery. fn. extend ({test: function () {return this. each (function () {alert (this. nodeName)}) ;}}); jQuery ("body *"). click (function () {$ (this ). test (); // call the jQuery object method });

In a word, jQuery. extend is a custom extension of the JQuery class, And jQuery. fn. extend is a custom extension of the JQuery object.

Related Article

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.