原型——可遍歷的 invoke() 方法



該方法是每種() 方法或 collect() 方法的最常用情況下的最佳化。針對所有元素呼叫相同的方法,並使用相同潛在引數。

語法

Iterator.invoke(methodName[, arg...]);

返回值

返回方法呼叫的結果。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            alert(['hello', 'world', 'cool!'].invoke('toUpperCase') );
            // Returns ['HELLO', 'WORLD', 'COOL!'];

            alert(['hello', 'world', 'cool!'].invoke('substring', 0, 3));
            // Returns ['hel', 'wor', 'coo']
         }
      </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.