← Back to jQuery Course | Chapter 11: Utility Methods | Lesson 4 of 6

extend() से Objects Merge करना

extend method एक object की properties दूसरे में copy करता है। settings combine करने के लिए यह उपयोगी है।
Syntax
javascript
var merged = $.extend({}, object1, object2);
var deep = $.extend(true, {}, object1, object2);

extend() क्या है?

$.extend() एक या ज़्यादा source objects की properties को एक target object पर copy करता है, उन्हें एक single combined object में merge करते हुए।

उदाहरण: What is extend()?

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      const merged = $.extend({a: 1}, {b: 2});
      console.log(merged);
    </script>
  </body>
</html>

Properties को Override करना

जब दो source objects same property name share करती हैं, तो argument list में बाद में आने वाला object जीत जाता है और पहले वाली value को overwrite कर देता है।

उदाहरण: Override Properties

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      const result = $.extend({color: "red"}, {color: "blue"});
      console.log(result.color);
    </script>
  </body>
</html>

Default Options इस्तेमाल करना

एक बहुत common pattern यह है कि sensible fallback values वाला एक defaults object define किया जाए, फिर एक final settings object produce करने के लिए $.extend({}, defaults, userOptions) call किया जाए।

उदाहरण: Use Default Options

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      const defaults = {speed: 300, color: "blue"};
      const userOptions = {color: "red"};
      const settings = $.extend({}, defaults, userOptions);
      console.log(settings);
    </script>
  </body>
</html>

कई Objects Merge करना

आप एक ही $.extend() call में दो से ज़्यादा objects merge कर सकते हैं, और properties सब में left से right copy होती हैं।

उदाहरण: Merge Multiple Objects

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      const merged = $.extend({}, {a: 1}, {b: 2}, {c: 3});
      console.log(merged);
    </script>
  </body>
</html>

एक छोटा extend() Project

extend() reusable, configurable functions बनाने के लिए खासतौर पर valuable है, क्योंकि यह हर caller को सिर्फ वही specific options override करने देता है जिनकी उसे परवाह है।

उदाहरण: Small extend() Project

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      function createWidget(options) {
      const settings = $.extend({speed: 300, color: "blue"}, options);
      console.log(settings);
      }
      createWidget({color: "green"});
    </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. $.extend(defaults, options) call करना और original defaults खो देना, जबकि पहला argument बदल दिया जाता है; $.extend({}, defaults, options) इस्तेमाल करें।
  2. यह उम्मीद करना कि nested objects एक shallow $.extend() से merge हो जाएँगी, जबकि nested object पूरी तरह replace हो जाता है जब तक आप deep merge के लिए पहले true pass न करें।
  3. arguments को गलत order में रखना, जैसे $.extend({}, options, defaults), ताकि defaults user के options को overwrite कर दें।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.