返回到:Prototype – 枚举
此方法查找迭代器返回 true 的第一个元素。此方法别名为 find() 方法。
语法
Iterator.detect([context]);
返回值
返回迭代器返回 true 的第一个元素,否则未定义。
例子
HTML
x
26
26
1
<html>
2
<head>
3
<title>Prototype examples</title>
4
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
5
6
<script>
7
function isPrime(n) {
8
if (2 > n) return false;
9
if (0 == n % 2) return (2 == n);
10
for (var index = 3; n / index > index; index += 2)
11
if (0 == n % index) return false;
12
return true;
13
} // isPrime
14
function showResult() {
15
alert($R(10,15).detect(isPrime) );
16
}
17
</script>
18
</head>
19
20
<body>
21
<p>Click the button to see the result.</p>
22
<br />
23
<br />
24
<input type = "button" value = "Result" onclick = "showResult();"/>
25
</body>
26
</html>
返回到:Prototype – 枚举
阅读剩余 39%
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8635.html