SoFunction
Updated on 2025-04-12

Prototype User Guide

Enumerable is an abstract object (it should be noted that there is no concept of a class in JavaScript. The class it refers to is a function. Inheritance generally refers to an object (parent) copying its method attribute (pass, copy is referenced) into the prototype attribute (object) of the subclass (function). Enumerable cannot be used directly. It is inherited by many enumeration types (Hash, Array, Range, etc.). The inherited types must implement a _each method to provide a specific type enumeration method. Enumerable provides the following methods for other subclasses:each(iterator):Iterator is a function object. This method calls the specific type of _each method to call iterator on each object it contains. For example, if Enumerable specifically refers to an Array, eg: var a=[2,3,4], then the (iterator) method will call iterator(2,0), iterator(3,1), and iterator(4,3), in turn, where the second parameter refers to the index. This method is used in almost every method in EnumerableeachSlice(number, iterator):Separate each of the Enumerable type objects by number, for example, [1,2,3,4,5].eachSlice(3)=>[[1,2,3],[4,5]], no iterator is provided, then iterator=: function(k){return k}, many iterators in Prototype have this default value, or: function() {}, which actually returns [iterator([1,2,3]),iterator([4,5])]all(iterator):Call iterator on each value in the Enumerable type. If one of them returns false, it returns false. Otherwise, it returns true, which is equivalent to determining whether each value executes iterator is "true"any(iterator):Contrary to all, determine whether each value is "false" (whether there is a value that is true)collect(iterator)/map:Call iterator for each value, and the result is composed of a new array to returndetect(iterator)/find:Call iterator for each value. If there is one that is not false, it returns the value that is not false after executing iterator (not the value after executing iterator), which is equivalent to finding the first true value.findAll(iterator)/select:equivalent to detect, but find all the true values ​​and return an arraygrep(pattern, iterator):Returns the value that matches the pattern. If iterator provides, it returns the value of the execution iterator.include(object)/member:Whether an object is included in the arrayinGroupsOf(number, fillWith):The mutated version of eachSlice is separated by number. If the length of the last value of the separated array is less than number, fill with fillwith, for example [1,2,3,4,5].inGroupsOf(3)=>[[1,2,3],[4,5,null]]inject(memo, iterator):injectioninvoke(method):Callmax(iterator):Maximum valuemin(iterator):Minimum valuepartition(iterator):Separationpluck(property):collectionreject(iterator):Unqualified products, the opposite of findAllsortBy(iterator):According to the iterator sort, if the object called is Array, just call the built-in sort(iterator)toArray()/entries:Return each value of the call object into an arrayzip():For example [2,3,4].zip([5,6,7])=>[[2,5],[3,6],[4,7]], if the last parameter type is function, it will return [iterator([2,5]),iterator([3,6]),iterator([4,7])],inspect():The string representation of the Enumerable object is NND. It turns out that Enumerable has so many functions. I feel that the author has learned Ruby too much. He moved all the methods to Prototype. It makes us study harder. The Prototype file is getting bigger and bigger, wasting bandwidth. Alas, I found that many of the functions are becoming more and more difficult to understand. Let’s take a look at the source code to understand it. I don’t know how to express it. I hope you will just regard this article as an irregular reference. If you have any problems, it depends on the source code to understand it. Otherwise, I will not be responsible if it misleads you.