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


相关文章
摄影测量学:课后练习总结2
摄影测量学:课后练习总结2
531 0
摄影测量学:课后练习总结2
|
人工智能 搜索推荐 算法
人工智能在医疗诊断中的应用与前景
本文探讨了人工智能在医疗诊断中的最新进展、应用案例以及面临的挑战。通过分析AI在医学影像识别、电子病历分析和个性化治疗方案等领域的具体应用,揭示了其提高诊断准确性、缩短诊断时间的潜力。同时,讨论了数据隐私、算法偏见等伦理和法律问题,并提出了未来研究方向。
|
边缘计算 5G vr&ar
|
安全 大数据 5G
运营商在安全领域的优势分析
近年来,中美贸易摩擦加剧,国际争端凸显,国家高度重视网络安全的建设,网络安全已上升及国家战略层面,网络空间作为“第五疆域”受到极大重视。个人与企业对于网络安全的需求不断增加,中国网络安全市场也随之发展。
413 0
|
机器学习/深度学习 TensorFlow 算法框架/工具
交叉验证(Cross-Validation)
交叉验证(Cross-Validation)是一种常用的评估机器学习模型性能的技术。它通过将数据集分为训练集和验证集,并多次重复这个过程,以获得对模型性能的更准确估计。
542 2
|
Windows
Windows删除文件时如何取消显示“确认是否删除”的弹窗
本文介绍Windows电脑删除文件时,开启或取消显示确认删除这一提示弹窗的方法~
1045 1
Windows删除文件时如何取消显示“确认是否删除”的弹窗
|
弹性计算 网络安全
阿里云服务器ECS更换公网IP地址的方法教程
阿里云服务器ECS更换公网IP地址的方法教程
422 0
|
机器学习/深度学习 算法 开发者
回归模型的显著性检验-1| 学习笔记
快速学习回归模型的显著性检验-1。
回归模型的显著性检验-1| 学习笔记
|
机器学习/深度学习 数据采集 弹性计算
使用阿里云服务器能做什么?十大用途来了
阿里云服务器可以干什么?服务器的用途有很多,常见的有网站、小程序、视频服务器、手机APP等,例如微信基于腾讯云服务器,淘宝基于阿里云服务器,阿里云百科来说下阿里云服务器十大用途:
4698 0
使用阿里云服务器能做什么?十大用途来了
|
编解码
【经典蓝牙】蓝牙AVRCP协议分析
蓝牙AVRCP协议是蓝牙设备之间音视频的控制协议。定义了音频/视频的控制、浏览、查询、通知等一系列的命令集。常用来蓝牙耳机对手机的音乐进行控制,以及获取手机的音乐信息等场景。AVRCP协议有两个角色,分别是controller(CT)和 target(TG)。CT: 发送控制命令到对端,控制对端媒体播放器的设备,例如蓝牙耳机,蓝牙遥控器等。TG:接收对端的控制命令,并执行操作,进行回复的设备,例如手机,电脑等。
4682 0
【经典蓝牙】蓝牙AVRCP协议分析