原型 - 可列舉 collect() 方法



此方法返回對每個元素應用迭代器的結果。此方法別名為 map() 方法。

可選 context 引數是將迭代器函式繫結到的引數。如果使用,迭代器中 this 關鍵字將指向引數給出的物件。

語法

Iterator.collect([context]);

返回值

返回對每個元素應用迭代器的結果。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            alert($R(1,5).collect(function(n) { return n * n; }) );
            // This returns [1, 4, 9, 16, 25]
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      <br />
      <br />
      <input type = "button" value = "Result" onclick = "showResult();"/>
   </body>
</html>

輸出

prototype_enumerating.htm
廣告