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


相关文章
|
存储 安全 数据库
数据库必知词汇:分级存储
分级存储是将数据采取不同的存储方式分别存储在不同性能的存储设备上,减少非重要性数据在一级本地磁盘所占用的空间,还可加快整个系统的存储性能。
1477 0
|
11月前
|
人工智能 监控 安全
多领域实用工具,提升效率与协作的法宝
精选多领域实用工具,提升效率与协作。TinyTask:32KB轻量自动化工具,适合基础重复操作;Qdown:基于Aria2内核的全能下载器,内存占用仅30MB;燃精灵:微信空号检测软件,助你精准抓取潜在客户;Prezi:非线性动态演示工具,支持AI生成大纲与多人协作;向日葵:跨平台远程控制,提供安全高效的远程办公解决方案。善用这些工具,优化工作流程,释放生产力。
224 0
|
8月前
|
机器学习/深度学习 编解码 计算机视觉
MATLAB实现人脸识别检测与标出图片中人脸
MATLAB实现人脸识别检测与标出图片中人脸
291 0
|
SQL 人工智能 关系型数据库
【PG锦囊】阿里云 RDS PostgreSQL 版插件—AI 插件(rds_ai)
本文介绍了AI 插件(rds_ai)的核心优势、适用场景等,帮助您更好地了解 rds_ai 插件。想了解更多 RDS 插件信息和讨论交流,欢迎加入 RDS PG 插件用户专项服务群(103525002795)
|
机器学习/深度学习 数据采集 自然语言处理
2022年必须要了解的20个开源NLP 库(一)
2022年必须要了解的20个开源NLP 库(一)
1604 0
2022年必须要了解的20个开源NLP 库(一)
|
Cloud Native 前端开发 Docker
云原生之使用Docker部署Ghost个人博客
云原生之使用Docker部署Ghost个人博客
463 2
|
Scala
spinal HDL - 05 - Spinal HDL - 函数和时钟域
spinal HDL - 05 - Spinal HDL - 函数和时钟域
525 0
spinal HDL - 05 - Spinal HDL - 函数和时钟域
|
机器学习/深度学习 人工智能 边缘计算