Comparison of Jquery.extend () and JQuery.fn.extend ()

Source: Internet
Author: User

      • Same point
      • Different points
        • Jqueryextend
        • Jqueryfnextend
      • code example

Same point

Both are used to extend jquery and to add methods to jquery, which are often used in the development of jquery-related plugins.

Different points jquery.extend ()

Adding a class method to the JQuery class itself is a static method;

Calls to the added method can only be invoked by the jquery itself, and the jquery instance object cannot invoke the newly added method. For example, the show and hide methods are added, and are called by: Jquery.show () and Jquery.hide ().

JQuery.fn.extend ()

Look at the source code of jquery to see: Jquery.fn=jquery.prototype, it is not difficult to find that Jquery.fn is actually the prototype of jquery. Then calling JQuery.fn.extend () is actually the way to extend the Jquery.prototype object. Since all the member methods in the jquery prototype are inherited by all jquery instance objects, there is a way to add the jquery instance object as long as it is.

Unlike Jquery.extend (), JQuery.fn.extend () is adding a method to a jquery prototype, which is a member method of the jquery instance object.

The invocation has also changed, and all jquery instance objects can be called without invoking the added method via jquery itself.

code example
<! DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title>Jquery.extend ()</title></head><body><div id="Sample">Extend</div></body><script src="Jquery-1.11.0.js"></script><script>$ (document). Ready ( function($){Jquery.extend ({show: function(){Alert"Show message!"); }, Hide: function () {Alert"Hide message!");        }        });        Jquery.hide (); Jquery.show ();//Use jquery itself to invoke the newly added method}) JQuery ( function($){JQuery.fn.extend ({show: function(){Alert"Show message!"); }, Hide: function () {Alert"Hide message!");        }        }); $("#sample"). Show (); $("#sample"). Hide ();//Using a jquery instance object to invoke the newly added method})</script></html>

Comparison of Jquery.extend () and JQuery.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.