Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>From the Docs of <a href="http://docs.jquery.com/Plugins/Authoring" rel="nofollow">Plugins/Authoring - jQuery Wiki</a></h3> <p>To write a jQuery plugin, start by adding a new function property to the jQuery.fn object where the name of the property is the name of your plugin: </p> <pre><code>jQuery.fn.myPlugin = function() { // Do your awesome plugin stuff here }; </code></pre> <p>The above one is the simplest of the plugin making. Although there is a specific steps to follow, using a closure is the best practice to avoid any <code>$</code> dollar sign conflict with any other libraries like mootools, prototype etc.</p> <p>To make sure that your plugin doesn't collide with other libraries that might use the dollar sign, it's a best practice to pass jQuery to an IIFE (Immediately Invoked Function Expression) that maps it to the dollar sign so it can't be overwritten by another library in the scope of its execution. </p> <pre><code> (function( $ ) { $.fn.myPlugin = function() { // Do your awesome plugin stuff here }; })(jQuery); </code></pre> <p><strong>Now within that closure, we can use the dollar sign in place of jQuery as much as we like.</strong></p> <h2>Namespacing :</h2> <p>Properly namespacing your plugin is a very important part of plugin development. Namespacing correctly assures that your plugin will have a very low chance of being overwritten by other plugins or code living on the same page. Namespacing also makes your life easier as a plugin developer because it helps you keep better track of your methods, events and data. </p> <h2>Plugin Methods</h2> <pre><code>(function( $ ){ $.fn.tooltip = function( options ) { // THIS }; $.fn.tooltipShow = function( ) { // IS }; $.fn.tooltipHide = function( ) { // BAD }; $.fn.tooltipUpdate = function( content ) { // !!! }; })(jQuery); </code></pre> <p>This is a discouraged because it clutters up the <code>$.fn</code> namespace. To remedy this, you should collect all of your plugin's methods in an <code>object literal</code> and call them by passing the string name of the method to the plugin. </p> <pre><code> (function( $ ){ var methods = { init : function( options ) { // THIS }, show : function( ) { // IS }, hide : function( ) { // GOOD }, update : function( content ) { // !!! } }; $.fn.tooltip = 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 on jQuery.tooltip' ); } }; })( jQuery ); // calls the init method $('div').tooltip(); // calls the init method $('div').tooltip({ foo : 'bar' }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload