Prototype - Enumerable max() 方法



該方法返回最大元素(或基於元素的計算),如果列舉為空,則返回 undefined。元素要麼直接比較,要麼先應用迭代器並比較返回的值。

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

語法

Iterator.max(context);

返回值

返回最大值。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            alert("$R(1,10).max() : " +  $R(1,10).max() );
            // Returns 10

            function Person(name, age) {
               this.name = name;
               this.age = age;
            }
            var john = new Person('John', 20);
            var mark = new Person('Mark', 35);
            var daisy = new Person('Daisy', 22);

            alert("Max Age:"+[john, mark, daisy].max(function(person) {
               return person.age ;
            }) );
         }
      </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
廣告