Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstand jQuery Tutorial Module Pattern example
    primarykey
    data
    text
    <p>Considering the last code fragment (reported below) about module pattern published in the <a href="http://learn.jquery.com/code-organization/concepts/" rel="nofollow">Code Organization section of the Learning Center</a>, I'm trying to understand some aspect of the module in the example:</p> <ul> <li>The variable declarations (<code>$items</code>, <code>$container</code>...) are separated by <code>;</code> while the function declarations (<code>createContainer</code>, <code>buildUrl</code>, <code>showItem</code>, ....) are separated by <code>,</code>. Why? Is there a bracket problem?</li> <li>Why the name of the first three variables (<code>$items</code>, <code>$container</code> and <code>$currentItem</code>) starts with <code>$</code>? Does this imply a naming convention (since <code>javascript</code> allows the character <code>$</code>) used to identify a DOM fragment variable or is there some other reason?</li> <li>Why the function <code>createContainer</code> is declared using <code>var</code> while the other functions (<code>buildUrl</code>, <code>showItem</code>,...) doesn't have <code>var</code>?</li> </ul> <p>// Using the module pattern for a jQuery feature $( document ).ready(function() {</p> <pre><code>var feature = (function() { var $items = $("#myFeature li"); var $container = $("&lt;div class='container'&gt;&lt;/div&gt;"); var $currentItem = null; var urlBase = "/foo.php?item="; var createContainer = function() { var $i = $( this ); var $c = $container.clone().appendTo( $i ); $i.data( "container", $c ); }, buildUrl = function() { return urlBase + $currentItem.attr("id"); }, showItem = function() { var $currentItem = $( this ); getContent( showContent ); }, showItemByIndex = function( idx ) { $.proxy( showItem, $items.get( idx ) ); }, getContent = function( callback ) { $currentItem.data("container").load( buildUrl(), callback ); }, showContent = function() { $currentItem.data("container").show(); hideContent(); }, hideContent = function() { $currentItem.siblings().each(function() { $( this ).data("container").hide(); }); }; $items.each( createContainer ).click( showItem ); return { showItemByIndex: showItemByIndex }; })(); feature.showItemByIndex( 0 ); }); </code></pre>
    singulars
    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.
 

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