Prototype source code analysis-enumerable

Source: Internet
Author: User
Prototype source code analysis-enumerable

Keywords: Prototype

Js Code

 

 
  1. Var $ break = new Object (); // The break Object can be used in comparison with java exception.
  2. Var $ continue = new Object (); // The Object indicating the continue can be used in comparison with the exception of java
  3. Var Enumerable = {
  4. Each: function (iterator) {// execute the _ each function
  5. Var index = 0;
  6. Try {
  7. This. _ each (function (value ){
  8. Try {
  9. Iterator (value, index ++ );
  10. } Catch (e ){
  11. If (E! = $ Continue) Throw E;
  12. }
  13. });
  14. } Catch (e ){
  15. If (E! = $ Break) Throw E;
  16. }
  17. },
  18. // Boolean returns the summary result after function traversal. If no false value is returned for each execution, true is returned.
  19. ALL: function (iterator ){
  20. Var result = true;
  21. This. each (function (value, index ){
  22. Result = result &&!! (Iterator | Prototype. K) (value, index );
  23. If (! Result) throw $ break;
  24. });
  25. Return result;
  26. },
  27. // Boolean returns the result of the function traversal. If the result is not false, true is returned.
  28. Any: function (iterator ){
  29. Var result = true;
  30. This. each (function (value, index ){
  31. If (result = !! (Iterator | prototype. K) (value, index ))
  32. Throw $ break;
  33. });
  34. Return result;
  35. },
  36. // Result array puts the result after function traversal into an array and returns
  37. Collect: function (iterator ){
  38. VaR Results = [];
  39. This. Each (function (value, index ){
  40. Results. Push (iterator (value, index ));
  41. });
  42. Return results;
  43. },
  44. // Return the value of the non-false parameter in the any function.
  45. Detect: function (iterator ){
  46. Var result;
  47. This. each (function (value, index ){
  48. If (iterator (value, index )){
  49. Result = value;
  50. Throw $ break;
  51. }
  52. });
  53. Return result;
  54. },
  55. // Filter the collect function. Only the value of the function parameter that executes the non-false function enters the result array.
  56. Findall: function (iterator ){
  57. VaR Results = [];
  58. This. Each (function (value, index ){
  59. If (iterator (value, index ))
  60. Results. Push (value );
  61. });
  62. Return results;
  63. },
  64. // Filter the collect function. Only when the execution value conforms to pattern can the execution result be entered into the result array.
  65. Grep: function (pattern, iterator ){
  66. Var results = [];
  67. This. each (function (value, index ){
  68. Var stringValue = value. toString ();
  69. If (stringValue. match (pattern ))
  70. Results. push (iterator | Prototype. K) (value, index ));
  71. })
  72. Return results;
  73. },
  74. // Whether boolean is a member of the value Array
  75. Include: function (object ){
  76. Var found = false;
  77. This. each (function (value ){
  78. If (value = object ){
  79. Found = true;
  80. Throw $ break;
  81. }
  82. });
  83. Return found;
  84. },
  85. // Calculates the sum of inject injection results.
  86. Inject: function (memo, iterator ){
  87. This. each (function (value, index ){
  88. Memo = iterator (memo, value, index );
  89. });
  90. Return memo;
  91. },
  92. // Pass the function + required parameters for traversal and execution and return the execution result set
  93. Invoke: function (method ){
  94. Var args = $ A (arguments). slice (1 );
  95. Return this. collect (function (value ){
  96. Return value [method]. apply (value, args );
  97. });
  98. },
  99. // Obtain the maximum return value after function traversal.
  100. Max: function (iterator ){
  101. Var result;
  102. This. each (function (value, index ){
  103. Value = (iterator | Prototype. K) (value, index );
  104. If (value> = (result | value ))
  105. Result = value;
  106. });
  107. Return result;
  108. },
  109. // Obtain the minimum returned value after function traversal.
  110. Min: function (iterator ){
  111. VaR result;
  112. This. Each (function (value, index ){
  113. Value = (iterator | prototype. K) (value, index );
  114. If (value <= (result | value ))
  115. Result = value;
  116. });
  117. Return result;
  118. },
  119. // Return the execution result according to true and non-true as a two-dimensional array
  120. Partition: function (iterator ){
  121. VaR Trues = [], falses = [];
  122. This. each (function (value, index ){
  123. (Iterator | Prototype. K) (value, index )?
  124. Trues: falses). push (value );
  125. });
  126. Return [trues, falses];
  127. },
  128. // Retrieve the attribute value of each element from the traversal array and put it into the result array.
  129. Pluck: function (property ){
  130. Var results = [];
  131. This. each (function (value, index ){
  132. Results. push (value [property]);
  133. });
  134. Return results;
  135. },
  136. // Returns the combination of non-real elements in the iterator result of the traversal execution function.
  137. Reject: function (iterator ){
  138. VaR Results = [];
  139. This. Each (function (value, index ){
  140. If (! Iterator (value, index ))
  141. Results. Push (value );
  142. });
  143. Return results;
  144. },
  145. // Return the sorting result set after the sorting function is executed.
  146. SortBy: function (iterator ){
  147. Return this. collect (function (value, index ){
  148. Return {value: value, criteria: iterator (value, index )};
  149. }). Sort (function (left, right ){
  150. Var a = left. criteria, B = right. criteria;
  151. Return a <B? -1: a> B? 1: 0;
  152. }). Pluck ('value ');
  153. },
  154. // You can traverse the set and convert it into a js array.
  155. ToArray: function (){
  156. Return this. collect (Prototype. K );
  157. },
  158. // Zip Compression
  159. Zip: function (){
  160. Var iterator = Prototype. K, args = $ A (arguments );
  161. If (typeof args. last () = 'function ')
  162. Iterator = args. pop ();
  163. Var collections = [this]. concat (args). map ($ );
  164. Return this. map (function (value, index ){
  165. Iterator (value = collections. pluck (index ));
  166. Return value;
  167. });
  168. },
  169. // View similar tostring
  170. Inspect: function (){
  171. Return '# this. toArray (). inspect () +'> ';
  172. }
  173. }
  174. Object. extend (Enumerable ,{
  175. Map: Enumerable. collect,
  176. Find: Enumerable. detect,
  177. Select: Enumerable. findAll,
  178. Member: Enumerable. include,
  179. Entries: Enumerable. toArray
  180. });

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.