JQuery $.extend () Usage Summary

Source: Internet
Author: User

1.Query for the development of the plug-in arch two methods, namely:
1.jquery.extend (object); To extend the JQuery class itself. Adds a new method to the class.
2.jquery.fn.extend (object); Adds a method to a JQuery object.

Jquery.fn
Jquery.fn = Jquery.prototype = {    init:function (selector, context) {        // content     }}

2.Jquery.fn = Jquery.prototype. O (∩_∩) o haha ~, to this prototype (prototype) is not strange!!

3. Although JavaScript There is no clear concept of a class, but it is more convenient to use a class to understand it. jquery is a well-encapsulated class, such as we use the statement $ ("#div1") to generate an instance of the jquery class.

Jquery.extend (object)

Adding a class method to the jquery class can be understood as adding a static method.

Chestnut ①

jquery.extend ({    min:function (A, b) {        return a < b? a:b;    },    max:function (A, B ) {        return a > B? a:b;    }}); Jquery.min (23//  Jquery.max (45 //   5
JQuery.fn.extend (object);

is to add a "member function" to the jquery class. An instance of the jquery class can call this "member function".

Chestnut ②

For example, we want to develop a plugin, make a special edit box, when it is clicked, then alert the contents of the current edit box. You can do this:

$.fn.extend ({    alertwhileclick:function () {        $ (this). Click (function () {            alert ($ (  this). Val ()     );}); //$ ("#input1") is jquery's instance, call this extension method $ ("#input1"). Alertwhileclick ();

The call to Jquery.extend () does not extend the method to an instance of the object, and the method that references it is also implemented by the JQuery class, such as Jquery.init ()

The call to JQuery.fn.extend () extends the method to the prototype of the object, so when instantiating a jquery object, it has these methods in jquery. JS in the everywhere this point

jQuery.fn.extend = JQuery.prototype.extend

You can extend an object into the prototype of jquery, which is the plugin mechanism .

Chestnut ③

(function ($) {    = function (options) {};     // equivalent to Var    tooltip = {        function (options) {}    };     = $.prototype.extend (tooltip) = $.fn.tooltip}) (jQuery);

Excerpt from:http://caibaojian.com/jquery-extend-and-jquery-fn-extend.html

JQuery $.extend () Usage Summary

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.