title: What does (function($) {})(jQuery); mean?
body: <p>I am just starting out with writing jQuery plugins. I wrote three small plugins but I have been simply copying the line into all my plugins without actually knowing what it means. Can someone tell me a little more about these? Perhaps an explanation will come in handy someday when writing a framework :)</p>
  
  <p>What does this do? (I know it extends jQuery somehow but is there anything else interesting to know about this)</p>
  
  <pre><code>(function($) {
  
  })(jQuery);
  </code></pre>
  
  <p>What is the difference between the following two ways of writing a plugin:</p>
  
  <p><strong>Type 1:</strong></p>
  
  <pre><code>(function($) {
      $.fn.jPluginName = {
  
          },
  
          $.fn.jPluginName.defaults = {
  
          }
  })(jQuery);
  </code></pre>
  
  <p><strong>Type 2:</strong></p>
  
  <pre><code>(function($) {
      $.jPluginName = {
  
          }
  })(jQuery);
  </code></pre>
  
  <p><strong>Type 3:</strong></p>
  
  <pre><code>(function($){
  
      //Attach this new method to jQuery
      $.fn.extend({ 
  
          var defaults = {  
          }  
  
          var options =  $.extend(defaults, options);  
  
          //This is where you write your plugin's name
          pluginname: function() {
  
              //Iterate over the current set of matched elements
              return this.each(function() {
  
                  //code to be inserted here
  
              });
          }
      }); 
  })(jQuery);
  </code></pre>
  
  <p>I could be way off here and maybe all mean the same thing. I am confused. In some cases, <strong>this</strong> doesn't seem to be working in a plugin that I was writing using Type 1. So far, Type 3 seems the most elegant to me but I'd like to know about the others as well.</p>