JQuery function declaration

What is the difference between $ .myfunc and $ .fn.myfunc? I see that I need to return the value and use $ (). Myfunc () to call, and the other not. Can the community offer me a link or keywords to get more understanding? Thanks.

+4
source share
2 answers

$. myfunc refers to a "static" or global function in the jquery namespace. It does not depend on jquery initialization with a selector such as $ ('# id'). Myfunc (...).

$. ajax is an example.

$. fn.myfunc, on the other hand, adds myfunc to the prototype of the jquery object, so when the jquery objetc is created using the $ ('# id') selector, the new object has the myfunc method, which is invokable in the context of the jquery object just created.

+7
source

$.fn is short for jQuery.prototype . It increments a jQuery object. It is used when working with a set of elements selected using the selector.

 $('a').newWindow(); 

I believe that assigning the property directly to $ will make it a utility function, for example each() (not tied to a specific set of matched elements).

 var sum = $.arraySum(array); 
+4
source

All Articles