原型 - Enumerable each() 方法



此方法是 Enumerable 的基石。你可以使用通用方式迭代所有元素,然後返回 Enumerable,從而允許鏈式呼叫。

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

語法

Iterator.find([context]);

返回值

返回一個可列舉元素

示例

<html>
   <head>
      <title>Prototype examples</title> 
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            ['one', 'two', 'three'].each(function(s) {
               alert("First loop : " + s);
            });

            [ 'hello', 'world'].each(function(s, index) {
               alert("Second Loop : " +  index + ': ' + s);
            });
         }
      </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
廣告
© . All rights reserved.