作者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