`

【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. });   

 

 

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

分享到:
评论

相关推荐

    Prototype Enumerable对象 学习第1/2页

    Enumerable是Prototype框架的基石,而Enumerable不单独使用,在Prototype中其它对象mix了Enumerable里面的方法,这样就可以在这些对象上应用Enumerable的方法,这样的对象有:Array,Hash,ObjectRange,还有一些和...

    Prototype使用指南之enumerable.js

    Enumerable是一个抽象对象(需要说明的是,javascript中并没有类的概念,所指的类也就是一个函数,继承一般指的是一个对象(父)将它的方法属性copy(通过Object.extend, copy的是引用)到子类(函数)的prototype属性(一...

    Prototype源码浅析 Enumerable部分之each方法

    在javascript中,根本找不到Enumerable的影子,因为这一块是Prototype作者从Ruby中借鉴过来的。并且Enumerable在实际中根本没有直接应用的机会,都是混入到其他的对象中,可以说是其他对象的一个“父类”(不过只是...

    Prototype源码浅析 Enumerable部分(二)

    剩下的方法太多,于是分作两部分。亮点就是$break和$continue,以及grep方法的思想

    前端开源库-node-enumerable

    前端开源库-node-enumerable节点可枚举,ES6就绪LINQ功能以typescript编写

    通过Enumerable实现线程控制(源码 更新)

    根据AsyncEnumerator的原理改写,提供了线程函数的流程控制,线程管理,监视函数调用超时的功能。 适用于需要开启多个线程,要去线程能够长时间稳定执行某一操作的场景 不足之处欢迎留言

    Recursively Enumerable Sets and Degrees(Soare)

    Recursively Enumerable Sets and Degrees by Rovert I. Soare。递归论的书籍,Soare的,从djvu转过来的,有可能有误。经典的教材

    prototype手册

    prototype 精妙的文档 • Extensions for the Object class • Extensions for the Number class • Extensions for the Function class • Extensions for the String class ...• The Enumerable object

    Prototype使用指南之array.js

     将iterable转化为数组,如果iterable定义了toArray方法,就调用这个方法,否则利用iterable的length属性进行枚举, 如果iterable没有length属性的话就返回空数组[] Array对象除了扩展Enumerable对象的方法外,...

    CSharp 4.0 .Net Framework V4.0 Enumerable 类

    CSharp 4.0 .Net Framework V4.0 Enumerable 类

    Enumerable.js:适用于所有类型集合的实用函数

    这个库很大程度上受到 Ruby 的 Enumerable 模块的启发,并借用了它的大量功能。 注意:这并不意味着是一个独立的库,而是要与具有.each方法的另一种数据类型结合使用。安装作为 NPM 模块npm install enumerable-js ...

    Prototype Hash对象 学习

    var Hash = Class.create(Enumerable, (function() { //初始化,创建一个新的Hash对象 function initialize(object) { this._object = Object.isHash(object) ? object.toObject() : Object.clone(object); } //...

    arguments:永远不要写“Array.prototype.slice.call(arguments);” 以后再!

    这是基于但使用Object.defineProperty(arguments.constructor.prototype, [functionName], {enumerable: false, configurable: true, value: [functionBody]})代替,以避免迭代时出现混乱。 Object....

    Prototype ObjectRange对象学习

    ObjectRange对象基本就是实现了连续的数字或者字符串,其中只包含一个方法,include,判断某个数字...并且ObjectRange对象还混入了Enumerable的方法,所以可以直接在ObjectRange对象上调用Enumerable对象里面的方法。

    enumerable.lua:一个lua集合库

    local Enumerable = require ( ' enumerable ' ) collectionInstance = Enumerable. create ({ 1 , 2 , 3 , 4 , 5 }) secondElement = collectionInstance[ 2 ] collectionInstance: each ( function ( value , ...

    enumerable-methods

    该项目展示了Ruby Enumerable模块中方法的重建列表。 重写的Enumerable方法是: 每个-&gt; my_each each_with_index-&gt;​​ my_each_with_index 选择-&gt; my_select 全部? -&gt; my_all? 任何? -&gt; my_any? 没有...

    C#使用LINQ中Enumerable类方法的延迟与立即执行的控制

    主要介绍了C#的LINQ查询中Enumerable类方法的延迟与立即执行,LINQ语言集成查询可以让C#和VB以查询数据库相同的方式操作内存数据,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics