SyntaxStudy
Sign Up
jQuery Plugin Namespacing Best Practices
jQuery Intermediate 3 min read

Plugin Namespacing Best Practices

Plugin Namespacing

Namespace your plugin to avoid conflicts. Use a prefix, namespace your events, and prefix CSS classes to prevent collisions with other plugins.

Example
// Plugin namespace: "myco"
$.fn.mycoCarousel = function() { /* ... */ };

// Namespaced events
$el.on("click.myco-carousel", handler);
$el.off(".myco-carousel"); // remove all myco events

// Namespaced CSS classes
// .myco-carousel-wrap
// .myco-slide
// .myco-active
Pro Tip

Pick a short company or project prefix and use it consistently across all plugins.