← Back to jQuery Course | Chapter 14: Custom Plugins | Lesson 4 of 4

Plugin Best Practices

Plugin names specific और समझने में आसान होने चाहिए। ऐसे names से बचें जो दूसरे plugins से conflict कर सकें।
Syntax
javascript
$.fn.pluginName = function(options) {
  var settings = $.extend({option: defaultValue}, options);
  return this.each(function() {
    // work on $(this) using settings
  });
};

Clear Names इस्तेमाल करें

एक अच्छी तरह से named plugin init या update जैसे generic, easily-collided names से बचता है, और इसके बजाय इतना specific name इस्तेमाल करता है जो describe करे कि यह असल में।

उदाहरण: Use Clear Names

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <style>
      .carousel-active { border: 2px solid blue; }
    </style>
  </head>
  <body>
    <div class="carousel">Slide</div>
    <script>
      $.fn.imageCarousel = function() {
        return this.addClass("carousel-active");
      };
      $(".carousel").imageCarousel();
    </script>
  </body>
</html>

Plugins को Chainable रखें

किसी plugin से हमेशा this return करें जब भी वह selected elements पर operate करे, क्योंकि यही वह चीज़ है जो बाद में call पर दूसरे jQuery methods को chain होने देती है।

उदाहरण: Keep Plugins Chainable

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div class="box">Box</div>
    <script>
      $.fn.myPlugin = function() {
      return this.css("color", "blue");
      };
      $(".box").myPlugin().fadeIn();
    </script>
  </body>
</html>

Defaults और Options इस्तेमाल करें

किसी plugin के options के लिए हमेशा sensible default values दें, और caller द्वारा supply किए गए options को उन defaults के साथ merge करें — आमतौर पर $.extend( इस्तेमाल करके।

उदाहरण: Use Defaults and Options

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div class="box">Box</div>
    <script>
      $.fn.myPlugin = function(options) {
      const settings = $.extend({speed: 300}, options);
      return this.fadeIn(settings.speed);
      };
      $(".box").myPlugin();
    </script>
  </body>
</html>

Scope Protect करें और Conflicts से बचें

किसी plugin के internal helper variables को private रखने के लिए एक closure इस्तेमाल करें, और plugin को खुद एक genuinely unique name दें, क्योंकि दोनों practices इसे कम करती हैं।

उदाहरण: Protect Scope and Avoid Conflicts

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      (function($) {
      $.fn.uniquePluginName = function() {
      return this.css("color", "green");
      };
      })(jQuery);
    </script>
  </body>
</html>

Validate करें और Document करें

एक trustworthy plugin अपने inputs को हमेशा सही मानने के बजाय validate करता है, और अपने options तथा expected usage को clearly document करता है।

उदाहरण: Validate and Document

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div class="box">Box</div>
    <script>
      $.fn.myPlugin = function(options) {
      if (!options || !options.color) {
      console.log("myPlugin requires a color option");
      return this;
      }
      return this.css("color", options.color);
      };
      $(".box").myPlugin({color: "red"});
    </script>
  </body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी plugin का नाम $.fn.init या $.fn.update जैसा generic रखना, जो existing methods से collide करता है।
  2. each() के बाहर एक shared variable में state store करना, ताकि कई elements एक-दूसरे की settings overwrite कर दें।
  3. $.extend(defaults, options) से shared defaults object को mutate करना, जबकि इसे $.extend({}, defaults, options) होना चाहिए।
चैप्टर सारांश
  • मौजूदा plugins को किसी page में integrate किया जा सकता है।
  • jQuery prototype chain वह तरीका है जिससे plugins नए methods add करते हैं।
  • एक custom plugin लिखना reusable code के लिए best practices follow करता है।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.