Plugin design recommendations
Now that we have examined common ways to extend jQuery and jQuery UI by creating plugins, we can review and supplement what we've learned with a list of recommendations.
Protect the
$
alias from potential interference from other libraries by usingjQuery
instead or passing$
into an immediately invoked function expression (IIFE) so that it can be used as a local variable.Whether extending the jQuery object with
$.myPlugin
or the jQuery prototype with$.fn.myPlugin
, add no more than one property to the$
namespace. Additional public methods and properties should be added to the plugin's namespace (for example,$.myPlugin.publicMethod
or$.fn.myPlugin.pluginProperty
).Provide a map of default options for the plugin:
$.fn.myPlugin.defaults = {size: 'large'}
.Allow the plugin user to optionally override any of the default settings for all subsequent calls to the method (
$.fn.myPlugin.defaults.size = 'medium';
) or for a single call ($('div').myPlugin({size: 'small'...