原型 - 可列舉的 eachSlice() 方法



此方法將項分成塊,基於特定大小,且最後一塊可能較小。

可選上下文引數是迭代器函式將繫結到的內容。如果使用,則迭代器內的 this 關鍵字將指向由引數給定的物件。

語法

Iterator.eachSlice([context]);

返回值

返回切片陣列(陣列)。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         var students = [
            { name: 'Sunny', age: 20 },  { name: 'Audrey', age: 21 },
            { name: 'Matt', age: 20 },   { name: 'Lodie', age: 26 },
            { name: 'Will', age: 21 },   { name: 'David', age: 23 },
            { name: 'Julien', age: 22 }, { name: 'Thomas', age: 21 },
            { name: 'Serpil', age: 22 }
         ];

         function showResult() {
            alert ( students.eachSlice(4, function(toon) {
               return toon.pluck('name');
            }) );
            var arr = students.eachSlice(2).first();
               arr.each(function(s) {
               var h = $H(s);
               alert("Value : " + h.inspect());
            });
         }
      </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
廣告