Understanding jquery's $.extend (), $.fn, and $.fn.extend ()

Source: Internet
Author: User

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

JQuery.fn.extend ();

Jquery.extend ();

Jquery.fn

Jquerr.fn=jquery.prototype={  function(selector,context) {   //  ....   }}

Jquery.fn = jquery.prototype is an extension of the method of the JQuery object (prototype)

Jquery.extend (object) is the addition of a class method to the jquery class, which can be understood as adding a static method. Such as:

function return a < b?  functionreturn a > B? a:b;}}); Jquery.min (  ////  5

Object jquery.extend (target,object1,[objectn])

Extend an object with one or more other objects, returning the object being extended

var false, Limit:5, Name: "foo"vartrue, Name: "Bar"true, Limit:5, Name: "Bar"}

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, it will have to alert the contents of the current edit box. You can do this:

 $.fn.extend ({              alertwhileclick:function() {                      $ (this). Click(  function() {                                  alert ((this). Val ());});}       );       $ (//   on the page:   

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

The call to Jquery.extend () does not extend the method to an instance of the object, and the method that references it needs to be implemented by the jquery class, such as Jquery.init (), and the Call of Jquery.extend () extends the method to the prototype of the object. , so it has these methods when instantiating a jquery object.

JQuery.fn.extend () = JQuery.prototype.extend ()

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

(functionfunction(options) {}; // equivalent to var tooltip = {function= $.prototype.extend (tooltip) = $.fn.tooltip}) ( JQuery);

Understanding jquery's $.extend (), $.fn, and $.fn.extend ()

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.