title: Extending jQuery plugin and changing default values
body: <p>I have a plugin called "myPlugin" which uses the commonly used design pattern of default values which can be overridden upon initialization.</p>
  
  <p>Instead of passing the overriding values upon initialization, I would like to extend the plugin as "myPlugin2" and change the default values so that when I initiate the extended plugin, it already has the desired new default values.</p>
  
  <p>I've played around with adding new methods to the extended plugin, but I can't figure out how to change the default values.</p>
  
  <p>In other words, I want the two lines of code to provide identical results.</p>
  
  <pre><code>$("body").myPlugin({'prop1':'prop1 modified','default_func4':function () {console.log('default_func4 modified')}});
  $("body").myPlugin2();
  </code></pre>
  
  <p>How can I extend a jQuery plugin and change the default values?</p>
  
  <p><a href="http://jsfiddle.net/L1ng37wL/2/" rel="nofollow">http://jsfiddle.net/L1ng37wL/2/</a></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
          &lt;title&gt;Testing&lt;/title&gt;  
          &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js" type="text/javascript"&gt;&lt;/script&gt;
          &lt;style type="text/css"&gt;
          &lt;/style&gt; 
          &lt;script type="text/javascript"&gt; 
              (function ($) {
  
                  var defaults = {
                      'prop1'  : 'prop1',
                      'default_func4'  : function () {console.log('default_func4');},
                      'default_func5'  : function () {console.log('default_func5');}
                  };
                  var methods = {
                      init: function (options) {
                          console.log("init");
                          console.log('defaults',defaults);
                          console.log('options',options);
                          var settings = $.extend({}, defaults, options);
                          console.log('settings',settings);
                          console.log('The value of "prop1" is '+settings.prop1);
                          settings.default_func4.call()
                      },
                      func1: function () {console.log("func1");},
                      func2: function () {console.log("func2");}
                  };
  
                  $.fn.myPlugin = function (method) {
                      // Method calling logic
                      if (methods[method]) {
                          return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
                      } else if (typeof method === 'object' || !method) {
                          return methods.init.apply(this, arguments);
                      } else {
                          $.error('Method ' + method + ' does not exist');
                      }
                  };
  
              })(jQuery);
  
              (function ($) {
  
                  var methods = {
                      'func1': function () {console.log("myPlugin2: func1");},
                      'func3': function () {console.log("myPlugin2: func3");}
                  }
                  $.fn.myPlugin2 = function (method) {
                      //HOW DO I CHANGE defaults.prop1 and defaults.default_func5?????
                      // Method calling logic
                      if (methods[method]) {
                          return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
                      } else if ((typeof method === 'object' || !method) &amp;&amp; methods.init) {
                          return methods.init.apply(this, arguments);
                      } else {
                          try {
                              return $.fn.myPlugin.apply(this, arguments);
                          } catch (e) {
                              $.error(e);
                          }
                      }
                  }
  
              })(jQuery);
  
              $(function(){
                  $("body").myPlugin({
                      'prop1':'prop1 modified',
                      'default_func4':function () {console.log('default_func4 modified')}
                  });
  
                  $("body").myPlugin2();
                  //$("body").myPlugin2('func1');
                  //$("body").myPlugin2('func2');
                  //$("body").myPlugin2('func3');
              });
  
          &lt;/script&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
      &lt;/body&gt; 
  &lt;/html&gt; 
  </code></pre>