惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
U
Unit 42
N
Netflix TechBlog - Medium
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
小众软件
小众软件
V
Visual Studio Blog
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
博客园 - 叶小钗
GbyAI
GbyAI
爱范儿
爱范儿
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
D
DataBreaches.Net
博客园_首页
D
Docker
A
About on SuperTechFans
G
Google Developers Blog
I
InfoQ
T
The Blog of Author Tim Ferriss
V
V2EX
博客园 - Franky

博客园 - laolaowhn

Clean code that works (simplicity is more complicity than you think 朴素比你想象的更复杂) jQuery in Action 中文版 第一章 jQuery简介 如翻译的不好请原谅 jQuery in Action 中文版 简介 专用卸载工具 Windows Installer Clean Up Uninstall 临时表 中文转拼音源代码 ([^<]*) - laolaowhn - 博客园 IIS6架设网站常见问题及症状举例答疑 - laolaowhn - 博客园 jquery验证表单,可以独立与文框,利jquery动态控制,源代码 vs2005 js 没有反应 - laolaowhn {JS}JavaScript使用正则表达式 JavaScript 正则表达式 验证网址 JavaScript下正则表达式密码强度验证 悟透JavaScript引子 prototype学习心得 Prototype 框架分析(一) Prototype.js的分析 我为什么选择mootools,抛弃了prototype. (mootools 与 prototype 核心代码分析) prototype源码分析
prototype源码分析—enumerable
laolaowhn · 2008-02-23 · via 博客园 - laolaowhn

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 != $continuethrow e;   
  13.         }   
  14.       });   
  15.     } catch (e) {   
  16.       if (e != $breakthrow e;   
  17.     }   
  18.   },   
  19.     返回函数遍历执行后的总结果 如果每次执行都返回非假则得到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.      
  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.     
  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. });