<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 介紹 迭代器模式(Iterator):提供一種方法順序一個聚合對象中各個元素,而又不暴露該對象內部表示。 迭代器的幾個特點是: 1. 訪問一個聚合對象的內容而無需暴露它的內部表示。 2. 為遍歷不同的集合結構提供一個統一的接口,從而支持同樣的算法在不同的集合結構上進行操作。 3. 遍歷的同時更改迭代器所在的集合結構可能會導致問題(比如C#的foreach里不允許修改item)。 ## 正文 一般的迭代,我們至少要有2個方法,hasNext()和Next(),這樣才做做到遍歷所有對象,我們先給出一個例子: ~~~ var agg = (function () { var index = 0, data = [1, 2, 3, 4, 5], length = data.length; return { next: function () { var element; if (!this.hasNext()) { return null; } element = data[index]; index = index + 2; return element; }, hasNext: function () { return index < length; }, rewind: function () { index = 0; }, current: function () { return data[index]; } }; } ()); ~~~ 使用方法和平時C#里的方式是一樣的: ~~~ // 迭代的結果是:1,3,5 while (agg.hasNext()) { console.log(agg.next()); } ~~~ 當然,你也可以通過額外的方法來重置數據,然后再繼續其它操作: ~~~ // 重置 agg.rewind(); console.log(agg.current()); // 1 ~~~ ## jQuery應用例子 jQuery里一個非常有名的迭代器就是$.each方法,通過each我們可以傳入額外的function,然后來對所有的item項進行迭代操作,例如: ~~~ $.each(['dudu', 'dudu', '酸奶小妹', '那個MM'], function (index, value) { console.log(index + ': ' + value); }); //或者 $('li').each(function (index) { console.log(index + ': ' + $(this).text()); }); ~~~ ## 總結 迭代器的使用場景是:對于集合內部結果常常變化各異,我們不想暴露其內部結構的話,但又響讓客戶代碼透明底訪問其中的元素,這種情況下我們可以使用迭代器模式。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看