<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之旅 廣告
                為數組中的每個元素執行指定操作。 ## 語法 ~~~ array1.forEach(callbackfn[, thisArg]) ~~~ ## 參數 |參數|定義| |--|--| |array1|必選。一個數組對象。| |callbackfn|必選。最多可以接受三個參數的函數。對于數組中的每個元素,forEach 都會調用 callbackfn 函數一次。 |thisArg|可選。 callbackfn 函數中的 this 關鍵字可引用的對象。如果省略 thisArg,則 undefined 將用作 this 值。 ## 異常 如果 callbackfn 參數不是函數對象,則將引發 TypeError 異常。 ~~~ Exception Condition ~~~ ## 備注 對于數組中出現的每個元素,forEach 方法都會調用 callbackfn 函數一次(采用升序索引順序)。將不會為數組中缺少的元素調用回調函數。 除了數組對象之外,forEach 方法可由具有 length 屬性且具有已按數字編制索引的屬性名的任何對象使用。 ### 回調函數語法 回調函數的語法如下所示: ~~~ function callbackfn(value, index, array1) ~~~ 你可使用最多三個參數來聲明回調函數。 回調函數的參數如下所示。 |回調參數|定義| |--|--| |Value|數組元素的值。| |index|數組元素的數字索引。| |array1|包含該元素的數組對象。| ### 修改數組對象 forEach 方法不直接修改原始數組,但回調函數可能會修改它。下表描述了在 forEach 方法啟動后修改數組對象所獲得的結果。 |forEach 方法啟動后的條件|元素是否傳遞給回調函數?| |--|--| |在數組的原始長度之外添加元素。|否。| |添加元素以填充數組中缺少的元素。|是,如果該索引尚未傳遞給回調函數。| |元素已更改。|是,如果該元素尚未傳遞給回調函數。| |從數組中刪除元素。|否,除非該元素已傳遞給回調函數。| 下面的示例闡釋了 forEach 方法的用法。 ~~~ // Define the callback function. function ShowResults(value, index, ar) { document.write("value: " + value); document.write(" index: " + index); document.write("<br />"); } // Create an array. var letters = ['ab', 'cd', 'ef']; // Call the ShowResults callback function for each // array element. letters.forEach(ShowResults); // Output: // value: ab index: 0 // value: cd index: 1 // value: ef index: 2 在下面的示例中,callbackfn 參數包含回調函數的代碼。 JavaScript // Create an array. var numbers = [10, 11, 12]; // Call the addNumber callback function for each array element. var sum = 0; numbers.forEach( function addNumber(value) { sum += value; } ); document.write(sum); // Output: 33 ~~~ 下面的示例闡釋了 thisArg 參數的用法,該參數指定可對其引用 this 關鍵字的對象。 ~~~ // Define the object that contains the callback function. var obj = { showResults: function(value, index) { // Call calcSquare by using the this value. var squared = this.calcSquare(value); document.write("value: " + value); document.write(" index: " + index); document.write(" squared: " + squared); document.write("<br />"); }, calcSquare: function(x) { return x * x } }; // Define an array. var numbers = [5, 6]; // Call the showResults callback function for each array element. // The obj is the this value within the // callback function. numbers.forEach(obj.showResults, obj); // Embed the callback function in the forEach statement. // The obj argument is the this value within the obj object. // The output is the same as for the previous statement. numbers.forEach(function(value, index) { this.showResults(value, index) }, obj); // Output: // value: 5 index: 0 squared: 25 // value: 6 index: 1 squared: 36 // value: 5 index: 0 squared: 25 // value: 6 index: 1 squared: 36 ~~~
                  <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>

                              哎呀哎呀视频在线观看