What is the difference between jQuery. fn. extend and jQuery. extend ?, Jquery. fn. extend

Source: Internet
Author: User

What is the difference between jQuery. fn. extend and jQuery. extend ?, Jquery. fn. extend

Let's talk about it first. On the Internet, all of them are reprinted and copied. Basically, there are only one or two articles that are unclear ....

Body:

To put it bluntly, we can see the clues from the two methods themselves.

We first regard jQuery as a class, so that we can better understand it.

JQuery. extend ()Is the extended jQuery class.

Let's assume that we think of jQuery as a kind of human beings. We can eat, drink, and dance. Now we use jQuery. extend to expand this classSingingSkills. In this way, whether it's a man, a woman, or a xx person... can inherit this skill (method.

We write as follows:

jQuery.extend({    liu: function(){        alert('liu');    }});

Then: $. liu (); to print the string "liu ".

The test code is as follows:

<!doctype html>

 

What does this mean? It means that. liu () has become the method (object) of the jQuery class. He can now "sing.

However, this capability can be used only by the jQuery class representing all mankind. You personally want to use it. You are Zhang San, Li Si, Wang Wu, Ma Liu. Can you represent all mankind?

So, this extension is also called a static method. It is only related to this class. It is irrelevant to your specific instantiated object.

Let's take a look.JQuery. fn. extend ()This method.

Literally, This expands the jQuery. fn method.

What is jQuery. fn?

The source code is written as follows:

 jQuery.fn = jQuery.prototype = {             init: function( selector, context ) {//….           //……          };   

 

Oh, it turns out that jQuery. fn = jQuery. prototype is a prototype.

That's easy to understand. jQuery. fn. extend expands the jQuery object (prototype) method!

What is the object? Is the class instantiation, such

$("#abc") 

This is an instantiated jQuery object.

That is to say, the jQuery. fn. extend extension method must be used on the jQuery object! He must be an instantiated object, such as John, Lee, four kings, five, and six.

To put it bluntly, we have to use this (assuming xyz () is an extended method ):

$('selector').xyz();

If you use $. xyz ();, an error is returned.

The example source code is as follows:

<!doctype html>

 

What is the difference between it and the above?

Actually, jQuery. extend () is mainly used to expand a global function, for example, $. ajax (), or expand a selector, for example, $. fn. each () when the selector is used.

Most plug-ins use jQuery. fn. extend ().

This is almost the case. This is my personal understanding. If you have a better opinion, please feel free to raise it.

 

Address: http://www.cnblogs.com/liu-l/p/3928373.html


What is the difference between $ extend () and $ fnmethodName in jquery?

$. Extend () creates a jQuery utility function. This function does not need to operate DOM elements, but is used to operate javascript non-element objects or perform other object-specific operations. for example, write a function to format a date.
$. Fn. methodName is used to create a new package method. It operates the jQuery package. Obviously, this method can be used in the body to represent the referenced package set (if each () is used () iterator, this indicates the DOM object of the current iteration), defining the new wrapper method has a very important rule: unless the method intentionally returns a specific value, otherwise, the returned value of the wrapper method should always be returned. this allows the new command to participate in the task jQuery chain.

In jquery, what is the difference between $ fn abc = function () {}and $ fnextend ({abc: function () {}})?

JQuery. fn = jQuery. prototype.

$. Fn. abc defines an abc method under the $. fn namespace and adds a method to the jQuery object.
$. Fn. extend to extend the jQuery class itself. Add a new method to the class.

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.