Confused: Why Pass a String as an Array Index in jQuery?
<p>I'm working through R Murphy's <a href="http://jqfundamentals.com/book/index.html" rel="nofollow">JQuery Fundamentals</a> and am trying to make sense of her solution: <a href="https://github.com/rmurphey/jqfundamentals/blob/master/solutions/slideshow.js" rel="nofollow">slideshow.js</a>.</p>

<p>She's created the following function (I've removed some of the navigation code in order to focus on the core functionality of fading images in and out):</p>

<pre><code>fadeCallback = function() {
            if (manualMode) { return; }

            var $this = $(this),
                $next = getItem($this, 'next'),
                num = $this.prevAll().length + 1;

            // set the timeout for showing
            // the next item in 5 seconds
            timeout = setTimeout(function() {
                showItem($this, $next);
            }, 5000);
        };
</code></pre>

<p>Inside fadeCallback she calls getItem() to get the next sibling of $this:</p>

<pre><code>getItem = function($item, trav) {
            var $returnItem = $item[trav]();
            return $returnItem.length ? 
                $returnItem : 
                $items[(trav == 'next') ? 'first' : 'last']();
        },
</code></pre>

<p>I'm getting lost by her use of 'next' in the second argument to getItem(). Is this a roundabout way of calling the .next() jQuery function on the first argument, '$(this)'?</p>

<p>If so, why not just call the function directly? i.e. $item.next();?</p>

<p><em><strong></em>*FOLLOW UP QUESTION*<em>*</em></strong></p>

<p>Maybe I've been looking at this code too long but it seems to me that inside getItem(), in the line:</p>

<pre><code>$items[(trav == 'next') ? 'first' : 'last']();
</code></pre>

<p>trav is never <strong>not</strong> 'next'. Yet by virtue of the code testing whether trav equals 'next' it implies that there are cases where trav != 'next'. But I can't identify any such case.</p>

<p>What am I not seeing?</p>

<p>Many thanks in advance,
n</p>
