- Prototype Tutorial
- Prototype - Home
- Prototype - Short Overview
- Prototype - Useful Features
- Prototype - Utility Methods
- Prototype - Element Object
- Prototype - Number Processing
- Prototype - Strings Processing
- Prototype - Array Processing
- Prototype - Hash processing
- Prototype - Basic Object
- Prototype - Templating
- Prototype - Enumerating
- Prototype - Event Handling
- Prototype - Form Management
- Prototype - JSON Support
- Prototype - AJAX Support
- Prototype - Expressing Ranges
- Prototype - Periodical Execution
- Prototype Useful Resources
- Prototype - Quick Guide
- Prototype - Useful Resources
- Prototype - Discussion
Prototype - $R() Method
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>
Output
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