Prototype JavaScript framework

class Hash

Description

A set of key/value pairs.

Hash can be thought of as an associative array, binding unique keys to values (which are not necessarily unique), though it can not guarantee consistent order its elements when iterating. Because of the nature of JavaScript, every object is in fact a hash; but Hash adds a number of methods that let you enumerate keys and values, iterate over key/value pairs, merge two hashes together, and much more.

Creating a hash

There are two ways to construct a Hash instance: the first is regular JavaScript object instantiation with the new keyword, and the second is using the $H function. There is one difference between them: if a Hash is passed to $H, it will be returned as-is, wherease the same hash passed to new Hash will be cloned instead.

Related utilities

$H

Constructor

new Hash([object])

Creates a new Hash. If object is given, the new hash will be populated with all the object's properties. See $H.

Instance methods

  • clone

    Hash#clone() -> Hash

    Returns a clone of hash.

  • get

    Hash#get(key) -> value

    Returns the value of the hash’s key property.

  • index

    Hash#index(value) -> String

    Returns the first key in the hash whose value matches value. Returns false if there is no such key.

  • inspect

    Hash#inspect() -> String

    Returns the debug-oriented string representation of the hash.

  • keys

    Hash#keys() -> [String...]

    Provides an Array of keys (that is, property names) for the hash.

  • merge

    Hash#merge(object) -> Hash

    Returns a new hash with object's key/value pairs merged in. To modify the original hash in place, use Hash#update.

  • set

    Hash#set(key, value) -> value

    Sets the hash’s key property to value and returns value.

  • toJSON

    Hash#toJSON() -> String

    Returns a JSON string.

  • toObject

    Hash#toObject() -> Object

    Returns a cloned, vanilla object.

  • toQueryString

    Hash#toQueryString() -> String

    Turns a hash into its URL-encoded query string representation.

  • unset

    Hash#unset(key) -> value

    Deletes the hash’s key property and returns its value.

  • update

    Hash#update(object) -> Hash

    Updates hash with the key/value pairs of object. The original hash will be modified. To return a new hash instead, use Hash#merge.

  • values

    Hash#values() -> Array

    Collect the values of a hash and returns them in an array.