I see the following two patterns quite often. What is the difference between the two? When is each appropriate?
$.pluginName = function(){}
and
$.fn.pluginName = function(){}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Very simple:
$.fn.pluginNameis a function callable on alljQuery.init* objects. This is useful for making chain-able functions to use with objects:Example:
To continue chaining objects, you need to return another
jQuery.initobject (could be the original one, or a filtered one):$.pluginNameis a function callable as$.pluginName(). This is useful for making utility functions, or storing a particular plugin’s default states.Example:
*The jQuery factory function (
jQuery()or$()) actually returns anew jQuery.initobject