且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

prototype中的$R函数的用法

更新时间:2022-09-25 08:25:56

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

Creates a new ObjectRange object. This method is a convenience wrapper around the ObjectRange constructor, but$R is the preferred alias.

ObjectRange instances represent a range of consecutive values, be they numerical, textual, or of another type that semantically supports value ranges. See the type's documentation for further details, and to discover how your own objects can support value ranges.

The $R function takes exactly the same arguments as the original constructor: the lower and upper bounds (value of the same, proper type), and whether the upper bound is exclusive or not. By default, the upper bound is inclusive.

 


  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2.  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html> 
  4.     <head> 
  5.         <title>$R</title> 
  6.         <script type="text/javascript" language="javascript" 
  7.          src="prototype.js" ></script> 
  8.         <script> 
  9.         // 依次输出1,2,3,4,为true时,右括号为开区间 
  10.         function test_R1(){ 
  11.             var range = $R(1, 5, true); 
  12.             range.each(function(value){ 
  13.                 alert(value); 
  14.             }); 
  15.         } 
  16.  
  17.         // 依次输出1,2,3,4,5,为false时,右括号为闭区间 
  18.         function test_R2(){ 
  19.             var range = $R(1, 5, false); 
  20.             range.each(function(value){ 
  21.                 alert(value); 
  22.             }); 
  23.         } 
  24.         function test(){ 
  25.             alert($R(0, 10).include(10)); 
  26.             // -> true 
  27.              alert($A($R(0, 5)).join(', ')); 
  28.             // -> '0, 1, 2, 3, 4, 5' 
  29.              alert($A($R('aa', 'ah')).join(', ')); 
  30.             // -> 'aa, ab, ac, ad, ae, af, ag, ah' 
  31.              alert($R(0, 10, true).include(10)) 
  32.             // -> false 
  33.             var arr = $R(0, 10, true).each(function(value) { 
  34.               // invoked 10 times for value = 0 to 9 
  35.             }) 
  36.             arr.each(function(value){ 
  37.                 alert(value); 
  38.             }); 
  39.         } 
  40.         </script> 
  41.     </head> 
  42.     <body> 
  43.         <form> 
  44.             <input type="button" value="click (exclusive = true)" 
  45.             onclick="test_R1()" /> 
  46.             <input type="button" value="click (exclusive = false)" 
  47.             onclick="test_R2()" /> 
  48.             <input type="button" value="click" onclick="test()"/> 
  49.         </form> 
  50.     </body> 
  51. </html> 

以下是官方特别让注意的:

Warning

Be careful with String ranges: as described in its String#succ method, it does not use alphabetical boundaries, but goes all the way through the character table:

 


  1. $A($R('a', 'e')) 
  2. // -> ['a', 'b', 'c', 'd', 'e'], no surprise there 
  3.  $A($R('ax', 'ba')) 
  4. // -> Ouch! Humongous array, starting as ['ax', 'ay', 'az', 'a{', 'a|', 'a}', 'a~'...] 

 


本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/410438,如需转载请自行联系原作者