[轉錄][JS]array.forEach
※ [本文轉錄自 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
06/22 15:07, 1F
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章