concept Hashes in category javascript

appears as: Hashes
Prototype and Scriptaculous in Action

This is an excerpt from Manning's book Prototype and Scriptaculous in Action.

Prototype takes a lot of its inspiration from Ruby, and Ruby supports multiple inheritance of sorts through a type of object called a mixin. A mixin provides implementations of behavior but is typically incomplete in some way. For example, the Enumerable mixin in Ruby provides a lot of higher-level functionality for working with array-like objects, but lacks the lower-level capability of iterating over the elements of the array. Any array-like structure, including Arrays, Hashes, and user-defined objects, can be enhanced by the Enumerable interface, provided they can offer a way of iterating over their members (which they do).

Most of the real power-user capabilities that Prototype provides for Array objects are defined within an object called Enumerable. The methods of this object are mapped onto the Array prototype. Because it is defined separately, it can also be mapped onto other array-like objects, notably the Hash and the ObjectRange, which we’ll look at later in this chapter. Purely as a point of interest, the inspiration for defining the Enumerable as a standalone object in this way comes from the Ruby language, in which Enumerable is defined as an abstract class that can be mixed into other classes such as Arrays and Hashes. We’ll demonstrate the Enumerable methods using Array objects in this section, and return to them again when we look at other Enumerable types.

In this section, we’ll introduce another of Prototype’s A-list functions, whose status is denoted by the brevity of the name. In the previous section, we saw how we could turn a variety of objects into Arrays using $A(). A similar function, called $H(), converts objects into Hashes. When $H() is invoked on an object, it adds all the Hash methods to the object. It also applies the Enumerable methods that we described in section 10.4, allowing us to reuse the entire set of functionality described previously on objects other than Arrays. As with $A(), invoking $H() on an object that is already a Hash will simply return it unchanged. Snippet 10.36 shows off a little of what $H() can do for us.