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 dollar ($) alias from potential interference from other libraries by using jQuery 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 an object containing default options ...

Get Learning jQuery - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.