Differences between jquery. FN. Extend and jquery. Extend

Source: Internet
Author: User

Jquery provides two methods for development plug-ins:

JavaScript code

  • Jquery. FN. Extend (object );
  • Jquery. Extend (object );

Jquery. Extend (object); adds a new method to the class to extend the jquery class itself.

Jquery. FN. Extend (object); add a method to the jquery object.

What is fn. It is not difficult to see jquery code.

JavaScript code

  • Jquery. fn = jquery. Prototype = {
  • Init: function (selector, context ){//....
  • //......
  • };

It turns out that jquery. fn = jquery. prototype. It is certainly no stranger to prototype.

Although JavaScript does not have a clear concept of a class, it is more convenient to use a class to understand it.

Jquery is a well-encapsulated class. For example, we use the statement $ ("# btn1") to generate a jquery class instance.

Jquery. Extend (object); adding class methods to the jquery class can be understood as adding static methods. For example:

XML/html code

  • $. Extend ({
  • Add: function (a, B) {return a + B ;}
  • });

Add a "static method" for jquery as add, and then you can use this method in the place where jquery is introduced,

JavaScript code
  • $. Add (3, 4); // return 7

Jquery. FN. Extend (object); the extension to jquery. prototype is to add "member functions" to the jquery class ". Jquery class instances can use this "member function ".

For example, we want to develop a plug-in and create a Special edit box. When it is clicked, the content in the current edit box of alert is displayed. You can do this:

JavaScript code
  • $. FN. Extend ({
  • Alertwhileclick: function (){
  • $ (This). Click (function (){
  • Alert ($ (this). Val ());
  • });
  • }
  • });
  • $ ("# Input1"). alertwhileclick (); // <input id = "input1" type = "text"/>

$ ("# Input1") is a jquery instance. When it calls the member method alertwhileclick, the extension is implemented. When it is clicked, the content in the current edit is displayed.

In the real development process, of course, there will not be such a small plug-in. In fact, jquery has provided a wide range of operations documents, events, CSS, Ajax, and effects methods, combined with these methods, you can develop more niubility plug-ins.


There is another special point here, that is, jquery at the beginning of the function. extend = jquery. FN. extend, and jquery. prototype is assigned to jquery. FN, so jquery will appear in subsequent calls. extend () and jquery. FN. different calls of extend () produce different results for these two methods, jquery. the call of extend () does not extend the method to the instance of the object. The method to reference it also needs to be implemented through the jquery class, such as jquery. init (), while jquery. FN. the call of extend () extends the method to the prototype of the object. Therefore, when instantiating a jquery object, it has these methods. This is very important in jquery. this is everywhere in JS

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.