- Prototype - Periodical Execution
- Prototype - Expressing Ranges
- Prototype - AJAX Support
- Prototype - JSON Support
- Prototype - Form Management
- Prototype - Event Handling
- Prototype - Enumerating
- Prototype - Templating
- Prototype - Basic Object
- Prototype - Hash processing
- Prototype - Array Processing
- Prototype - Strings Processing
- Prototype - Number Processing
- Prototype - Element Object
- Prototype - Utility Methods
- Prototype - Useful Features
- Prototype - Short Overview
- Prototype - Home
Prototype Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Prototype - Enumerating
Enumerable class provides a large set of useful methods for enumerations. Enumerations are objects that act as collection of values.
Enumeration methods are mostly used to enumerate arrays and hashes. There are other objects as well pke ObjectRange and various DOM- or AJAX-related objects where you can use enumeration methods.
The Context Parameter
Every method of Enumerable that takes an iterator also takes the context object as the next (optional) parameter. The context object is what the iterator is going to be binded to, so the this keyword inside it will point to the object.
var myObject = {}; [ foo , bar , baz ].each(function(name, index) { this[name] = index; }, myObject); // we have specified the context myObject;
This will produce the following result −
Output
{ foo: 0, bar: 1, baz: 2}
Using it Efficiently
When you need to invoke the same method on all the elements, go with invoke() method.
When you need to fetch the same property on all the elements, go with pluck() method.
The findAll/select methods retrieve all the elements that match a given predicate. Conversely, the reject() method retrieves all the elements that do not match a predicate. In the specific case where you need both the sets, you can avoid looping twice: just use partition() method.
Here is a complete pst of all the methods related to Enumerable.
Prototype Enumerable Methods
NOTE − Make sure you at least have the version 1.6 of prototype.js.
S.No. | Method & Description |
---|---|
1. | Determines whether all the elements are boolean-equivalent to true, either directly or through computation by the provided iterator. |
2. | Determines whether at least one element is boolean-equivalent to true, either directly or through computation by the provided iterator. |
3. | Returns the results of applying the iterator to each element. Apased as map(). |
4. | Finds the first element for which the iterator returns true. Apased by the find() method. |
5. | It lets you iterate over all the elements in a generic fashion, then returns the Enumerable, thereby allowing chain-calpng. |
6. | Groups items in chunks based on a given size, with last chunk being possibly smaller. |
7. | Apas for the more generic toArray method. |
8. | Finds the first element for which the iterator returns true. Convenience apas for detect(). |
9. | Returns all the elements for which the iterator returned true. Apased as select(). |
10. | Returns all the elements that match the filter. If an iterator is provided, it is used to produce the returned value for each selected element. |
11. | Groups items in fixed-size chunks, using a specific value to fill up the last chunk if necessary. |
12. | Determines whether a given object is in the Enumerable or not, based on the == comparison operator. Apased as member(). |
13. | Incrementally builds a result value based on the successive results of the iterator. |
14. | Optimization for a common use-case of each() or collect(): invoking the same method, with the same potential arguments, for all the elements. |
15. | Returns the results of applying the iterator to each element. Convenience apas for collect(). |
16. | Returns the maximum element (or element-based computation), or undefined if the enumeration is empty. Elements are either compared directly, or by first applying the iterator and comparing returned values. |
17. | Determines whether a given object is in the Enumerable or not, based on the == comparison operator. Convenience apas for include(). |
18. | Returns the minimum element (or element-based computation), or undefined if the enumeration is empty. Elements are either compared directly, or by first applying the iterator and comparing returned values. |
19. | Partitions the elements in two groups: those regarded as true, and those considered false. |
20. | Optimization for a common use-case of collect(): fetching the same property for all the elements. Returns the property values. |
21. | Returns all the elements for which the iterator returned false. |
22. | Apas for the findAll() method. |
23. | Returns the size of the enumeration. |
24. | Provides a custom-sorted view of the elements based on the criteria computed, for each element, by the iterator. |
25. | Returns an Array representation of the enumeration. Apased as entries(). |
26. | Zips together (think of the zip on a pair of trousers) 2 + sequences, providing an array of tuples. Each tuple contains one value per original sequence. |