Prototype $R() Method


Advertisements


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

Syntax

$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 the ending elements, otherwise it will not be included in the range.

Return Value

  • Range Object

Example

<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>

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
});

prototype_utility_methods.htm

Advertisements