原型 - $R() 方法



$R() 函式僅僅是 new ObjectRange(lowerBound, upperBound, excludeBounds) 的簡寫。

語法

$R(start, end[, exclusive = false]);

此處,start 是範圍的起始元素,end 是範圍的最後一個元素。如果將排他標誌設定為 false,則它將包含結束元素,否則結束元素將不包含在範圍內。

返回值

範圍物件。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function ShowValues() {
            var range = $R(10, 20, false);
            
            range.each(function(value, index) {
               alert(value);
            });
         }
      </script>
   </head>
   
   <body>
      <p>Click "Show Value" button to see the result</p>
      
      <form>
         <input type = "button" value = "Show Value" onclick = "ShowValues();"/>
      </form>
      
   </body>
</html>

輸出

更多示例

以下語句返回 true 值 -

$R(0, 10).include(10);

以下語句返回一個字串 "0, 1, 2, 3, 4, 5" -

$A($R(0, 5)).join(', ');

以下語句返回一個字串 "aa, ab, ac, ad, ae, af, ag, ah" -

$A($R('aa', 'ah')).join(', ');

以下語句返回 false -

$R(0, 10, true).include(10);

以下語句將為值 = 0 到 9 被呼叫 10 次 -

$R(0, 10, true).each(function(value) {
   // invoked 10 times for value = 0 to 9
});
prototype_utility_methods.htm
廣告
© . All rights reserved.