📅  最后修改于: 2020-10-21 08:45:14             🧑  作者: Mango
哈希可以看作是将唯一键绑定到值的关联数组。唯一的区别是您可以将任何字符串用作索引,而不仅仅是将数字用作索引。
构造Hash实例有两种方法-
要创建一个空哈希,您也可以调用任何不带参数的构造方法。
以下是显示如何以简单方式创建哈希,设置值和获取值的示例-
// Creating Hash
var myhash = new Hash();
var yourhash = new Hash( {fruit: 'apple'} );
var hishash = $H( {drink: 'pepsi'} );
// Set values in terms of key and values.
myhash.set('name', 'Bob');
// Get value of key 'name' as follows.
myhash.get('name');
yourhash.get('fruit');
hishash.get('drink');
// Unset a key & value
myhash.unset('name');
yourhash.unset('fruit');
hishash.unset('drink');
原型提供了广泛的方法来轻松评估哈希。本教程将通过适当的示例详细解释每种方法。
这是与哈希相关的所有方法的完整列表。
注意-确保至少具有1.6版的prototype.js。
S.No. | Method & Description |
---|---|
1. | clone()
Returns a clone of hash. |
2. | each()
Iterates over the name/value pairs in the hash. |
3. | get()
Returns the value of the hash key’s property. |
4. | inspect()
Returns the debug-oriented string representation of the hash. |
5. | keys()
Provides an Array of keys (that is, property names) for the hash. |
6. | merge()
Merges object to hash and returns the result of that merge. |
7. | remove()
Removes keys from a hash and returns their values. This method has been deprecated in version 1.6. |
8. | set()
Sets the hash key’s property to value and returns value. |
9. | toJSON()
Returns a JSON string. |
10. | toObject()
Returns a cloned, vanilla object. |
11. | toQueryString()
Turns a hash into its URL-encoded query string representation. |
12. | unset()
Deletes the hash key’s property and returns its value. |
13. | update()
Updates hash with the key/value pairs of object. The original hash will be modified. |
14. | values()
Collects the values of a hash and returns them in an array. |