How to create jQuery functions?

I have seen several different approaches to creating jQuery (-namespaced) functions, but I cannot say about the actual difference between them.

jQuery.fn.myFunction = function() { ... };

jQuery.myFunction = function() { ... };

jQuery.fn.extend({ myFunction: function() { ... } });

+4
source share
1 answer

jQuery.fn.myFunction is what you use to create functions that will be available for each jQuery result set:

 $('div').myFunction(); 

jQuery.myFunction is what you use to create helper functions that are only available for a jQuery object, like $.inArray

Your latest version extends the $.fn object with a new function and is the same functional equivalent of your first example.

+6
source

All Articles