返回到:Prototype – 枚举
此方法返回将迭代器应用于每个元素的结果。此方法别名为 map() 方法。
可选的上下文参数是迭代器函数将绑定到的内容。如果使用,迭代器中的this关键字将指向参数给定的对象。
语法
Iterator.collect([context]);
返回值
返回将迭代器应用于每个元素的结果。
例子
HTML
x
20
20
1
<html>
2
<head>
3
<title>Prototype examples</title>
4
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
5
6
<script>
7
function showResult() {
8
alert($R(1,5).collect(function(n) { return n * n; }) );
9
// This returns [1, 4, 9, 16, 25]
10
}
11
</script>
12
</head>
13
14
<body>
15
<p>Click the button to see the result.</p>
16
<br />
17
<br />
18
<input type = "button" value = "Result" onclick = "showResult();"/>
19
</body>
20
</html>
返回到:Prototype – 枚举
阅读剩余 31%
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8638.html