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

jQuery Prototype Chain

jQuery methods आमतौर पर $.fn के through add किए जाते हैं। यह उस prototype को refer करता है जो jQuery objects इस्तेमाल करती हैं।
Syntax
javascript
$.fn.methodName = function () {
  return this;
};

$.fn क्या है?

$.fn वह object है जिसे jQuery हर jQuery-wrapped selection के लिए prototype की तरह इस्तेमाल करता है, यही वजह है कि $.fn में एक method add करना उसे उपलब्ध बना देता है।

उदाहरण: What is $.fn?

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p>Text</p>
    <script>
      $.fn.shout = function() {
      return this.text($(this).text().toUpperCase());
      };
      $("p").shout();
    </script>
  </body>
</html>

Chaining के लिए this Return करें

एक plugin को आमतौर पर selected elements बदलना खत्म करने पर this return करना चाहिए, क्योंकि this उस jQuery object को refer करता है जिस पर method call किया गया था।

उदाहरण: Return this for Chaining

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p>Text</p>
    <script>
      $.fn.highlight = function() {
      return this.css("background", "yellow");
      };
      $("p").highlight().css("padding", "10px");
    </script>
  </body>
</html>

कई Elements के साथ काम करना

एक jQuery plugin को आमतौर पर current selection के हर element पर operate करना चाहिए, सिर्फ पहले पर नहीं, क्योंकि $(...) कई elements से match कर सकता है।

उदाहरण: Work with Multiple Elements

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p>One</p><p>Two</p>
    <script>
      $.fn.shout = function() {
      return this.each(function() {
      $(this).text($(this).text().toUpperCase());
      });
      };
      $("p").shout();
    </script>
  </body>
</html>

Plugin Scope को Safely इस्तेमाल करना

Plugin code को unnecessary global variables बनाने से बचना चाहिए, क्योंकि वे उसी page पर दूसरे scripts के साथ silently collide कर सकते हैं। एक plugin को wrap करना।

उदाहरण: Use Plugin Scope Safely

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

एक छोटा Prototype Project

यह prototype pattern — $.fn में functions attach करना जो chaining के लिए this return करते हैं — jQuery में नए chainable methods add करने का standard, expected तरीका है।

उदाहरण: Small Prototype Project

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p>Text</p>
    <script>
      $.fn.highlight = function() {
      return this.css("background", "yellow");
      };
      $("p").highlight();
    </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 के बजाय $ में add करना, ताकि $("p").myPlugin() एक function न हो।
  2. plugin के अंत में return this; भूल जाना, जो $("p").myPlugin().hide() जैसी chaining तोड़ देता है।
  3. plugin method के लिए एक arrow function इस्तेमाल करना, जहाँ this अब jQuery selection को refer नहीं करता।
🔒

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.