The role and difference of $.extend and $.fn.extend of jquery

Source: Internet
Author: User

jquery has two methods for developing plug-ins, namely:

JQuery.fn.extend ();

Jquery.extend ();

Although JavaScript does not have a clear class concept, it is more convenient to use a class to understand it. jquery is a well-encapsulated class, such as we use the statement $ ("#btn1") to generate an instance of the jquery class.

Jquery.extend (object); Add a " static method"to the jquery class. Such as:

1 jquery.extend ({2     functionreturn a < b a:b;},3     function return a > B? a:b;} 4 }); 5 //   26//  5
Static MethodsJquery.Extend(Target, Object1, [OBJECTN]) to extend an object with one or more other objects, returning the object being extended
1 var false, Limit:5, Name: "foo" }; 2 var true, Name: "Bar" }; 3 jquery.extend (settings, options); 4 true, Limit:5, Name: "Bar"}
extension Methods

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".

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:

1 $.fn.extend ({          2      alertwhileclick:function() {            3            $ ( this). Click (function() {                 4                   alert ($ (this). Val ());            5             });            6       }       7
member Methods$ ("#input1"). Alertwhileclick (); On the page is:

$ ("#input1") is a jquery instance, and when it calls the Member method Alertwhileclick, it implements the extension, which pops up the contents of the current edit each time it is clicked.

The role and difference of $.extend and $.fn.extend of 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.