Eight Weeks of Prototype: Week 7, Other Prototype Functionality
$$()), but there are a number of other useful functions available.
Retrieving a Form Element Value with $F()
In the previous section I showed you how to retrieve form values using either the form serialize() method or by using the
getValue() method. The
$F() function is an alias to
getValue() (although realistically it's not much of a shortcut).
Creating a Hash with $H()
As discussed in the third article in this series, the
Hash object is created using the
Listing 6 demonstrates this by creating a hash using an object, then reading and updating values.
Creating a Range with $R() and $A()
One class we haven't looked at yet is
ObjectRange. This is another type of
Enumerable object (see the third article in this series for more information about enumerables), used to represent a range of values. The reason I haven't touched on it sooner is that it's probably not something you will end up using very much.
You can create a new
ObjectRange by instantiating the class, however the preferred method is to use the
$R()function. This method accepts two arguments: the starting value and the ending value of the range.
Once you have a range you will often want to convert it to an array so it can be used just as a normal array would be. This is achieved using the
$A() function. You can think of using the combination of
$R() as simple array generation.
Listing 7 shows an example of creating a range of values using the
$R() method. The range is then converted to an array so the values can be easily manipulated.
Tokenizing a String with $w()
If you want to split a string into words (using whitespace as the delimiter), you can use the
$w() method (note the lower-case w).
Listing 8 shows an example of how this function could be used. A list of element IDs is passed to $w(), which we then use the enumerable each() method to hide and show all of those elements.
Your own mileage with this function will vary. It's not typically something I use but since it's a part of Prototype I have included it in this article.