Copyright ©

Prototype $R() Method

previous next


The $R() function is simply a short hand to writing new ObjectRange(lowerBound, upperBound, excludeBounds).


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

Here start is the starting element of the range and end is the last element of the range. If exclusive flag is set to false then it will include ending elements otherwise it will not be included in the range.

Return Value:


<title>Prototype examples</title>
<script type="text/javascript" 

function ShowValues()
   var range = $R(10, 20, false);
   range.each(function(value, index){



   <p>Click "Show Value" button to see the result</p>

   <input type="button" value="Show Value" onclick="ShowValues();"/>


To understand it in better way you can Try it yourself.

More Examples:

Following statement returns true value:

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

Following statement returns a string "0, 1, 2, 3, 4, 5":

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

Following statement returns a string "aa, ab, ac, ad, ae, af, ag, ah":

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

Following statement returns false:

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

Following statement will be invoked 10 times for value = 0 to 9:

$R(0, 10, true).each(function(value) {
  // invoked 10 times for value = 0 to 9

previous next

Copyright ©