Analysis of $.extend and $.fn.extend in jquery plug-in development

Source: Internet
Author: User

jquery plug-in development is divided into two types:


1 class level

Class level you can understand that to extend the jquery class, the most obvious example is $.ajax (...), which is equivalent to a static method.

The $.extend method, i.e. Jquery.extend (object), is used when developing its methods;

$.extend ({

Add: function (a,b) {return a+b;},

minus: function (a,b) {return a-b;}
});

Page Call:

var i = $.add (3,2);

var j = $.minus (3,2);

2 Object Level

The object level can be understood as an object based extension, such as $ ("#table"). ChangeColor (...); Here this ChangeColor, is based on the expansion of the object.

The $.fn.extend method, i.e. JQuery.fn.extend (object), is used when developing its methods;

$.fn.extend ({

Check: function () {
Return This.each ({
This.checked=true;
});
},
uncheck: function () {
Return This.each ({
This.checked=false;
});
}
});

Page Call:

$ (' input[type=checkbox] '). Check ();
$ (' input[type=checkbox] '). Uncheck ();


3, Extended

$.xy = {
Add:function (a,b) {return a+b;},
Minus:function (a,b) {return a-b;},
Voidmethod:function () {alert (' void ');}
};
var i = $.xy.add (3,2);
var m = $.xy.minus (3,2);
$.xy.voidmethod ();


Article from: http://blog.csdn.net/woshixuye/article/details/7246735

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.