使用Enumerable类 ( API: http://www.prototypejs.org/api/enumerable)
1.collect方法
<html>
2.each方法
<head>
<script src="prototype-1.6.0.2.js" type="text/javascript"></script>
<script>...
var a = [20,30,40,50];
alert(a.collect(function (value , index)
...{
return value * index;
}));
</script>
</html>
<html>
<head>
<script src="prototype-1.6.0.2.js" type="text/javascript"></script>
</script>
</head>
<body>
<div id="show">
<div>
</body>
<script>
var a = [20,30,40,50];
a.each(function (value , index)
...{
$("show").innerHTML += "index:" + index + " ,value:" + value + "<BR>";
});
</script>
</body>
</html>
3.inject方法
<html>
<head>
<script src="prototype-1.6.0.2.js" type="text/javascript"></script>
<script>...
var a = [20,30,40,50];
var b = a.inject(5 ,function (acc , value, index)
...{
return value * index + acc;
});
alert( b );
</script>
</html>
4.zip方法
<html>
<head>
<script src="prototype-1.6.0.2.js" type="text/javascript"></script>
</head>
<body>
<div id="show">
<div>
</body>
<script>...
$("show").innerHTML += [1,2,3].zip([4,5,6], [7,8,9]).inspect() + "<BR>";
$("show").innerHTML += [1,2].zip([4,5,6], [7,8,9]).inspect() + "<BR>";
$("show").innerHTML += [1,2,3].zip([4,5,6], [7,8]).inspect() + "<BR>";
</script>
</html>