← Back to jQuery Course | Chapter 16: jQuery vs Modern JS & Interview Prep | Lesson 6 of 6

Advanced jQuery Interview Questions

Event delegation एक parent पर एक handler attach करता है और matching child events handle करता है। यह dynamic elements के साथ अच्छे से काम करता है।

Event Delegation

Event delegation एक parent element से एक handler bind करता है और अपने descendants से events पकड़ने के लिए event bubbling पर depend करता है, बाद में add किए गए elements सहित।

उदाहरण: Event Delegation

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <ul id="list"><li>Item 1</li></ul>
    <script>
      $("#list").on("click", "li", function() {
      console.log("Delegated click:", $(this).text());
      });
    </script>
  </body>
</html>

Deferred और Promises

jQuery का Deferred object asynchronous operations के साथ काम करने का एक structured तरीका देता है, success, fail को handle करने के लिए done(), fail(), और always() expose करते हुए।

उदाहरण: Deferred and Promises

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      const dfd = $.Deferred();
      dfd.done(function() { console.log("Success"); }).fail(function() { console.log("Failure"); }).always(function() { console.log("Cleanup"); });
      dfd.resolve();
    </script>
  </body>
</html>

Performance और Caching

Advanced jQuery code को selections को variables में cache करके और कई updates को batch करके repeated selector calls और unnecessary DOM changes से बचना चाहिए।

उदाहरण: Performance and Caching

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Click</button>
    <script>
      const $btn = $("#btn");
      $btn.on("click", function() {
      $btn.css("color", "red");
      });
    </script>
  </body>
</html>

Security और XSS

Untrusted input — users या external systems से — को हमेशा raw HTML के रूप में insert करने के बजाय plain text की तरह treat किया जाना चाहिए। user input के साथ html() इस्तेमाल करने से बचें।

उदाहरण: Security and XSS

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="output"></div>
    <script>
      const userInput = "<img src=x onerror=alert(1)>";
      $("#output").text(userInput);
    </script>
  </body>
</html>

Advanced AJAX Patterns

Advanced AJAX code को explicitly loading states, network और HTTP errors, और returned data का shape handle करना चाहिए, बजाय यह मान लेने के कि हर request।

उदाहरण: Advanced AJAX Patterns

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="status"></div>
    <script>
      $("#status").text("Loading...");
      $.get("data.php").done(function(data) {
      $("#status").text(data.message);
      }).fail(function() {
      $("#status").text("Error loading data");
      });
    </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. event delegation explain करते समय यह न बताना कि यह event bubbling पर depend करता है, या यह भूल जाना कि किसी child पर stopPropagation() इसे रोक देता है।
  2. यह कहना कि .then() और .done() identical हैं, जबकि .then() values transform और chain कर सकता है, जबकि .done() सिर्फ एक callback register करता है।
  3. एक selector cache करना लेकिन DOM बदलने के बाद cache update न करना, ताकि stored jQuery object stale elements को refer करे।
चैप्टर सारांश
  • Native DOM और vanilla JS alternatives कई jQuery selectors और DOM methods की जगह ले सकते हैं।
  • jQuery AJAX और Fetch API अलग-अलग तरीकों से similar goals achieve करते हैं।
  • modern web work में jQuery कब इस्तेमाल करें यह जानना, और basic तथा advanced interview questions, आपको real situations के लिए तैयार करते हैं।

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.