簡體   English   中英

對象參數/函數和/或三元運算符混淆

[英]Object parameter/function and/or ternary operator confusion

我正在研究 R Murphy 的JQuery Fundamentals,並被困在她的解決方案中的一段代碼上: slideshow.js

她創建了以下函數(為了專注於圖像淡入淡出的核心功能,我刪除了一些導航代碼):

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);
        };

在fadeCallback 中,她調用getItem() 來獲取$this 的下一個兄弟:

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

在我看來,在 getItem() 中,在該行中...

$items[(trav == 'next') ? 'first' : 'last']();

...trav 永遠不會成為“下一個”。 即代碼中的其他任何地方我們都沒有將“trav”設置為“next”以外的任何內容。 然而,憑借測試 trav 是否等於 'next' 的代碼,它意味着存在 trav != 'next' 的情況。 但我無法確定任何此類情況。

我沒有看到什么?

$prevBtn = $('<input/>', {
    type: 'button',
    value: 'previous'
}).appendTo($controls),

$nextBtn = $('<input/>', {
    type: 'button',
    value: 'next'
}).appendTo($controls),

$prevBtn.bind('click', { direction : 'prev' }, handleBtnClick);
$nextBtn.bind('click', { direction : 'next' }, handleBtnClick);

handleBtnClick = function(e) {
    clearTimeout(timeout);

    manualMode = true;

    var $currentItem = $items.filter(':visible'),
        $itemToShow = getItem($currentItem, e.data.direction);

    showItem($currentItem, $itemToShow);
};

它顯然用於prev

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM