[JS]array.forEach

看板Web_Design作者 (knx)時間18年前 (2007/06/22 14:22), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
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
文章代碼(AID): #16UsgdGm (Web_Design)