Home > Web Front-end > JS Tutorial > Prototype usage guide enumerable.js_prototype

Prototype usage guide enumerable.js_prototype

WBOY
Release: 2016-05-16 19:21:21
Original
1187 people have browsed it

Enumerable is an abstract object (it should be noted that there is no concept of class in JavaScript, and the class referred to is a function. Inheritance generally refers to an object (parent) copying its method attributes (through Object.extend, Copy is a reference) to the prototype attribute (an object) of the subclass (function)) Enumerable cannot be used directly. It is inherited by many enumeration types (Hash, Array, Range, etc.), and the inherited types must implement an_ Each method provides specific types of enumeration methods. Enumerable provides the following methods for other subclasses: each(iterator): iterator is a function object. This method calls the _each method of a specific type to contain itself. Each object calls iterator. For example, if Enumerable specifically refers to an Array, eg: var a=[2,3,4], then the a.each(iterator) method will call iterator(2,0), iterator( 3,1), iterator(4,3), where the second parameter refers to the index. This method is used in almost every method in Enumerable eachSlice(number, iterator): Separates each Enumerable type object according to number, such as [1,2,3,4, 5].eachSlice(3)=>[[1,2,3],[4,5]], if no iterator is provided, then iterator=Prototype.K: function(k){return k}, many iterators in Prototype The default value is this, or Prototype.emptyFunction: function() {}. In fact, what is actually returned is [iterator([1,2,3]), iterator([4,5])] all( iterator): Call iterator for each value in the Enumerable type. If one of them returns false, it returns false, otherwise it returns true, which is equivalent to judging whether each value of the iterator is "true" any (iterator): Contrary to all, determine whether each value is "false" (whether one value is true) collect(iterator)/map: Call iterator for each value, and The results form a new array and return detect(iterator)/find: Call iterator on each value. If one of the values ​​is not false, return the value that is not false after executing the iterator (not return to execute the iterator value after), equivalent to finding the first true value findAll(iterator)/select: is equivalent to detect, but finding all true values ​​and returning an array grep(pattern, iterator ): Returns all values ​​that match the pattern. If the iterator is provided, it returns the value of the iterator. include(object)/member: Whether the array contains object inGroupsOf(number, fillWith): A variant version of eachSlice that separates objects by number. If the length of the last value of the separated array is less than number, it is filled with fillwith, such as [1,2,3,4,5].inGroupsOf(3) =>[[1,2,3],[4,5,null]] inject(memo, iterator): injectinvoke(method): call max( iterator): Maximum valuemin(iterator): Minimum valuepartition(iterator): Separationpluck(property): Collectionreject( iterator): Unqualified products, the opposite of findAllsortBy(iterator): Sort according to iterator, if the called object is Array, just call the built-in sort(iterator) directly toArray()/entries: Form each value of the calling object into an array and return zip(): 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 of the Enumerable object represents NND. It turns out that Enumerable has so many functions. I feel that the author has studied Ruby too much and used all methods. Moving on to Prototype, we have to study hard. Prototype files are getting bigger and bigger, which wastes bandwidth. I find that many of the functions become more difficult to understand the more they are explained. You should read more about the source code to understand. My writing skills are really poor, and some people really don’t know how to express them. I hope everyone will just regard this article as an informal reference. If you have any questions, you should read the source code to understand. Otherwise, I will not be responsible for misleading you.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template