[轉錄][JS]array.forEach

看板Ajax作者 (knx)時間17年前 (2007/06/22 14:23), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/1
※ [本文轉錄自 Web_Design 看板] 作者: knx (knx) 看板: Web_Design 標題: [JS]array.forEach 時間: Fri Jun 22 14:22:28 2007 Though not very well-known, Firefox supports a forEach method on arrays. Rather than for (var i = 0; i < array.length; i++) { do_something(array[i]); } , you can simply do array.forEach(function(element) { do_something(element); }); or more concisely: array.forEach(do_something); The callback function actually receives three arguments, though you will often ignore the last two. This example uses all of them: array.forEach(function(element, index, array) { GM_log("Element " + element + " in array " + array.join(", ") + " with index " + index); }); Within a forEach loop (being a function call), return can be used for the same effect as continue (in a for loop) and throw/catch can be used instead of break. Retrieved from "http://wiki.greasespot.net/Coding_tips" -- 甲: Linux? 我夠曉啦~~~ 乙: 你又曉?! 甲: 哼! 唔通我寫過 kernel 又話俾你知咩?! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.166.235.235 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.166.235.235

06/22 15:07, , 1F
定義在JS1.5,實作在JS1.6(Gecho1.8b2+)的東西
06/22 15:07, 1F
文章代碼(AID): #16UshJ31 (Ajax)
文章代碼(AID): #16UshJ31 (Ajax)