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


相关文章
|
人工智能 搜索推荐 算法
人工智能在医疗诊断中的应用与前景
本文探讨了人工智能在医疗诊断中的最新进展、应用案例以及面临的挑战。通过分析AI在医学影像识别、电子病历分析和个性化治疗方案等领域的具体应用,揭示了其提高诊断准确性、缩短诊断时间的潜力。同时,讨论了数据隐私、算法偏见等伦理和法律问题,并提出了未来研究方向。
|
边缘计算 5G vr&ar
|
机器学习/深度学习 监控 安全
智能混凝土:自我修复与环境感应的建筑材料
【10月更文挑战第21天】智能混凝土是一种集自我修复与环境感应于一体的先进建筑材料。通过复合智能型组分,智能混凝土能够实现自感知、自适应和自修复,显著提高结构的耐久性和安全性,减少维修成本,促进环保节能。未来,智能混凝土将向多功能化、智能化和绿色化方向发展,为建筑行业带来革命性变革。
|
存储 Kubernetes 容器
k8s创建NFS动态存储
k8s创建NFS动态存储
|
机器学习/深度学习 TensorFlow 算法框架/工具
交叉验证(Cross-Validation)
交叉验证(Cross-Validation)是一种常用的评估机器学习模型性能的技术。它通过将数据集分为训练集和验证集,并多次重复这个过程,以获得对模型性能的更准确估计。
553 2
|
存储 弹性计算 移动开发
2023阿里云无影云电脑使用教程分享(详细)
2023阿里云无影云电脑使用教程分享(详细)阿里云无影云电脑即无影云电脑,云电脑如何使用?云电脑购买后没有用户名和密码,先创建用户设置密码,才可以登录连接到云电脑。云电脑想要访问公网还需要开通互联网访问功能。阿里云百科来详细说下阿里云无影云电脑从购买、创建用户名密码和访问互联网全过程
1994 0
|
Windows
Windows删除文件时如何取消显示“确认是否删除”的弹窗
本文介绍Windows电脑删除文件时,开启或取消显示确认删除这一提示弹窗的方法~
1092 1
Windows删除文件时如何取消显示“确认是否删除”的弹窗
|
弹性计算 网络安全
阿里云服务器ECS更换公网IP地址的方法教程
阿里云服务器ECS更换公网IP地址的方法教程
427 0
|
JSON 小程序 JavaScript
微信小程序的目录解析--【浅入深出系列002】
微信小程序的目录解析--【浅入深出系列002】 这是我的CSDN 的文章 转过来,可能有些许错误。请留言
|
敏捷开发 存储 安全
敏捷测试四象限
敏捷测试四象限
678 0
敏捷测试四象限