Prototype User Guide-enumerable. js

Source: Internet
Author: User

Enumerable is an abstract object (it must be noted that there is no class concept in javascript, and the class referred to is also a function. Inheritance generally refers to an object (parent) copy its method property (through Object. extend, copy is referenced) to the prototype attribute (an object) of the subclass (function). Enumerable cannot be used directly. It is used by many enumeration types (Hash, Array, Range, etc) the inherited type must implement a _ each method. The Enumerable method of the specific type is provided as follows: Each (iterator ):Iterator is a function object. This method calls the _ each method of the specific type to call iterator for each object contained in itself. For example, if Enumerable refers to an Array, eg: var a = [2, 3, 4], then. the each (iterator) method calls the iterator (2, 0), iterator (3, 1), and iterator (4, 3) in sequence. The second parameter indicates the index. This method is used in almost every method in Enumerable. EachSlice (number, iterator ):Separate each Enumerable object by number, for example, [1, 2, 3, 4, 5]. eachSlice (3) => [[1, 2, 3], [4, 5]. If no iterator is provided, iterator = Prototype. k: function (k) {return k}, which is the default value of many iterator in Prototype, or Prototype. emptyFunction: function () {} Actually Returns [iterator ([, 3]), iterator ([])] All (iterator ):Call iterator for each value in the Enumerable type. If one of them returns false, false is returned. Otherwise, true is returned, which is equivalent to determining whether iterator is "true" for each value" Any (iterator ):Opposite to all, determine whether each value is "false" (whether a value is true) Collect (iterator)/map:Call iterator for each value to form a new array and return the result Detect (iterator)/find:Call iterator for each value. If one value is not false, return the value that is not false after the iterator is executed (not the value after the iterator is executed), which is equivalent to finding the first true value. FindAll (iterator)/select:It is equivalent to detect, but finds all the true values and returns an array. Grep (pattern, iterator ):Returns the pattern-compliant value. If provided by iterator, the value of iterator execution is returned. Include (object)/member:Whether the array contains objects InGroupsOf (number, fillWith ):The variant version of eachSlice separates objects by number. If the length of the last value of the separated array is smaller than number, fill it with fillwith, for example, [1, 2, 4, 5]. inGroupsOf (3) => [[1, 2, 3], [4, 5, null] Inject (memo, iterator ):Injection Invoke (method ):Call Max (iterator ):Maximum Value Min (iterator ):Minimum value Partition (iterator ):Separation Pluck (property ):Collection Reject (iterator ):For unqualified products, the opposite is true for findAll. SortBy (iterator ):Sort by iterator. If the called object is Array, directly call the built-in sort (iterator ). ToArray ()/entries:Returns an array of each value of the called object. Zip ():Example:,. Zip ([, 7]) => [[], [], [], []. If the last parameter type is function, [iterator ([2, 5]), iterator ([3, 6]), iterator ([4, 7])], Inspect ():The string of the Enumerable object represents NND. Since Enumerable has so many functions, I feel that the author has learned Ruby too much and moved all the methods to Prototype, we have learned a lot, and Prototype files are getting bigger and bigger, which is a waste of bandwidth. I find that many functions are more difficult to explain, so let's take a look at the source code to understand them, even though some users do not know how to express the text, I hope you can still take this article as an informal reference. If you have any questions, please refer to the source code for understanding, otherwise, I am not responsible for misleading you.

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.