【prototype学习】prototype源码分析—enumerable

js 代码

 

 
  1. var $break    = new Object();   //表示break的对象 可以对比java的exception的使用
  2. var $continue = new Object();   //表示continue的对象 可以对比java的exception的使用
  3.   
  4. var Enumerable = {   
  5.   each: function(iterator) {   //执行_each函数
  6.     var index = 0;   
  7.     try {   
  8.       this._each(function(value) {   
  9.         try {   
  10.           iterator(value, index++);   
  11.         } catch (e) {   
  12.           if (e != $continuethrow e;   
  13.         }   
  14.       });   
  15.     } catch (e) {   
  16.       if (e != $breakthrow e;   
  17.     }   
  18.   },   
  19.   //boolean   返回函数遍历执行后的总结果 如果每次执行都返回非假则得到true
  20.   all: function(iterator) {   
  21.     var result = true;   
  22.     this.each(function(value, index) {   
  23.       result = result && !!(iterator || Prototype.K)(value, index);   
  24.       if (!result) throw $break;   
  25.     });   
  26.     return result;   
  27.   },   
  28.    //boolean  返回函数遍历执行后的总结果 只要遇到执行返回非假则得到true  
  29.   any: function(iterator) {   
  30.     var result = true;   
  31.     this.each(function(value, index) {   
  32.       if (result = !!(iterator || Prototype.K)(value, index))    
  33.         throw $break;   
  34.     });   
  35.     return result;   
  36.   },   
  37.   //result array 把函数遍历执行后的结果放进一个数组返回    
  38.   collect: function(iterator) {   
  39.     var results = [];   
  40.     this.each(function(value, index) {   
  41.       results.push(iterator(value, index));   
  42.     });   
  43.     return results;   
  44.   },   
  45.   //   返回any函数中执行非假的那个参数value
  46.   detect: function (iterator) {   
  47.     var result;   
  48.     this.each(function(value, index) {   
  49.       if (iterator(value, index)) {   
  50.         result = value;   
  51.         throw $break;   
  52.       }   
  53.     });   
  54.     return result;   
  55.   },   
  56.   //   针对collect函数进行过滤 只有执行非假的函数参数value才进入结果数组
  57.   findAll: function(iterator) {   
  58.     var results = [];   
  59.     this.each(function(value, index) {   
  60.       if (iterator(value, index))   
  61.         results.push(value);   
  62.     });   
  63.     return results;   
  64.   },   
  65.   //   针对collect函数进行过滤 只有执行value符合pattern,才把执行结果进入结果数组   
  66.   grep: function(pattern, iterator) {   
  67.     var results = [];   
  68.     this.each(function(value, index) {   
  69.       var stringValue = value.toString();   
  70.       if (stringValue.match(pattern))   
  71.         results.push((iterator || Prototype.K)(value, index));   
  72.     })   
  73.     return results;   
  74.   },   
  75.   //boolean 是否为value的数组的成员   
  76.   include: function(object) {   
  77.     var found = false;   
  78.     this.each(function(value) {   
  79.       if (value == object) {   
  80.         found = true;   
  81.         throw $break;   
  82.       }   
  83.     });   
  84.     return found;   
  85.   },   
  86.   //通过注入inject 累及运算结果进行  累计运算    
  87.   inject: function(memo, iterator) {   
  88.     this.each(function(value, index) {   
  89.       memo = iterator(memo, value, index);   
  90.     });   
  91.     return memo;   
  92.   },   
  93.   //传递 函数+需要的参数 进行遍历执行 并返回执行结果集   
  94.   invoke: function(method) {   
  95.     var args = $A(arguments).slice(1);   
  96.     return this.collect(function(value) {   
  97.       return value[method].apply(value, args);   
  98.     });   
  99.   },   
  100.   // 取函数遍历执行后 最大的返回值   
  101.   max: function(iterator) {   
  102.     var result;   
  103.     this.each(function(value, index) {   
  104.       value = (iterator || Prototype.K)(value, index);   
  105.       if (value >= (result || value))   
  106.         result = value;   
  107.     });   
  108.     return result;   
  109.   },   
  110.   // 取函数遍历执行后 最小的返回值   
  111.   min: function(iterator) {   
  112.     var result;   
  113.     this.each(function(value, index) {   
  114.       value = (iterator || Prototype.K)(value, index);   
  115.       if (value <= (result || value))   
  116.         result = value;   
  117.     });   
  118.     return result;   
  119.   },   
  120.   //把执行结果按照true 和 非true 作为2维数组返回   
  121.   partition: function(iterator) {   
  122.     var trues = [], falses = [];   
  123.     this.each(function(value, index) {   
  124.       ((iterator || Prototype.K)(value, index) ?    
  125.         trues : falses).push(value);   
  126.     });   
  127.     return [trues, falses];   
  128.   },   
  129. //  遍历数组取每个元素的该属性值放进结果数组返回   
  130.   pluck: function(property) {   
  131.     var results = [];   
  132.     this.each(function(value, index) {   
  133.       results.push(value[property]);   
  134.     });   
  135.     return results;   
  136.   },   
  137.   //返回遍历执行函数iterator结果非真的元素的结合   
  138.   reject: function(iterator) {   
  139.     var results = [];   
  140.     this.each(function(value, index) {   
  141.       if (!iterator(value, index))   
  142.         results.push(value);   
  143.     });   
  144.     return results;   
  145.   },   
  146.   //返回排序函数执行后的排序结果集   
  147.   sortBy: function(iterator) {   
  148.     return this.collect(function(value, index) {   
  149.       return {value: value, criteria: iterator(value, index)};   
  150.     }).sort(function(left, right) {   
  151.       var a = left.criteria, b = right.criteria;   
  152.       return a < b ? -1 : a > b ? 1 : 0;   
  153.     }).pluck('value');   
  154.   },   
  155.   //可遍历集合转换成js数组   
  156.   toArray: function() {   
  157.     return this.collect(Prototype.K);   
  158.   },   
  159.   //zip压缩   
  160.   zip: function() {   
  161.     var iterator = Prototype.K, args = $A(arguments);   
  162.     if (typeof args.last() == 'function')   
  163.       iterator = args.pop();   
  164.   
  165.     var collections = [this].concat(args).map($A);   
  166.     return this.map(function(value, index) {   
  167.       iterator(value = collections.pluck(index));   
  168.       return value;   
  169.     });   
  170.   },   
  171.   //查看 类似tostring   
  172.   inspect: function() {   
  173.     return '#this.toArray().inspect() + '>';   
  174.   }   
  175. }   
  176.   
  177. Object.extend(Enumerable, {   
  178.   map:     Enumerable.collect,   
  179.   find:    Enumerable.detect,   
  180.   select:  Enumerable.findAll,   
  181.   member:  Enumerable.include,   
  182.   entries: Enumerable.toArray   
  183. });   

 

 

待完善,欢迎指点、意见、建议。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值