作者knx (knx)
看板Ajax
标题[转录][JS]array.forEach
时间Fri Jun 22 14:23:14 2007
※ [本文转录自 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
1F:推 ephesians:定义在JS1.5,实作在JS1.6(Gecho1.8b2+)的东西 06/22 15:07