prototype源碼分析—enumerable

來源:互聯網
上載者:User
prototype源碼分析—enumerable

關鍵字: prototype

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 != $continue) throw e;   
  13.         }   
  14.       });   
  15.     } catch (e) {   
  16.       if (e != $break) throw 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. });  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.