prototype中的$R函数的用法

简介:
$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,如需转载请自行联系原作者


相关文章
|
5天前
|
JavaScript 前端开发
在JavaScript中,函数原型(Function Prototype)是一个特殊的对象
【5月更文挑战第11天】JavaScript中的函数原型是一个特殊对象,它为所有函数实例提供共享的方法和属性。每个函数在创建时都有一个`prototype`属性,指向原型对象。利用原型,我们可以向所有实例添加方法和属性,实现继承。例如,我们定义一个`Person`函数,向其原型添加`greet`方法,然后创建实例`john`和`jane`,它们都能调用这个方法。尽管可以直接在原型上添加方法,但推荐在构造函数内部定义以封装数据和逻辑。
18 2
|
9月前
|
前端开发 API 网络架构
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(六)
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(六)
|
9月前
|
前端开发 网络架构
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(一)
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map
|
9月前
|
前端开发 网络架构
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(四)
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(四)
|
9月前
|
前端开发 JavaScript 网络架构
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(三)
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(三)
|
9月前
|
前端开发 网络架构
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(二)
ES6-ES11-第一部分-let、const、解构赋值、模板字符串、简化对象写法、箭头函数、函数参数默认值、rest 参数、扩展运算符、Symbol、迭代器、生成器、Promise、Set、Map(二)
|
JavaScript 前端开发
Function() 构造函数
Function() 构造函数
47 0
Array.prototype.includes() 原型调用用法案例讲解
Array.prototype.includes() 原型调用用法案例讲解
89 2
|
JavaScript 网络架构 索引
Arguments 对象与简易柯里化
Arguments 对象与简易柯里化
140 0
|
JavaScript 前端开发
函数原型中的 call 和 apply 方法的区别
它们是在 JavaScript 引擎内在实现的,因为属于Function.prototype,所以每个Function对象实例,也就是每个方法都有call, apply属性。它们的作用一样,只是使用方式不同。
112 0