CopyCode The Code is as follows: // tool function of the hash object
Function $ H (object ){
Return new hash (object );
};
VaR hash = Class. Create (enumerable, (function (){
// Initialize and create a new hash object
Function initialize (object ){
This. _ OBJECT = object. ishash (object )? Object. toobject (): object. Clone (object );
}
// Overwrite the methods in enumerable, which will be used when traversing hash objects
Function _ each (iterator ){
For (var key in this. _ OBJECT ){
VaR value = This. _ object [Key], pair = [key, value];
Pair. Key = key;
Pair. value = value;
Iterator (pair );
}
}
Function set (Key, value ){
Return this. _ object [Key] = value;
}
Function get (key ){
If (this. _ object [Key]! = Object. Prototype [Key])
Return this. _ object [Key];
}
Function unset (key ){
VaR value = This. _ object [Key];
Delete this. _ object [Key];
Return value;
}
Function toobject (){
Return object. Clone (this. _ OBJECT );
}
Function keys (){
Return this. Pluck ('key ');
}
Function values (){
Return this. Pluck ('value ');
}
// Return the key of the value
Function Index (value ){
VaR match = This. Detect (function (pair ){
Return pair. value = value;
});
Return match & Match. Key;
}
Function Merge (object ){
Return this. Clone (). Update (object );
}
// Update the original hash object and update the key-value pairs in the object parameter to the original hash object
Function Update (object ){
Return new hash (object). Inject (this, function (result, pair ){
Result. Set (pair. Key, pair. value );
Return result;
});
}
Function toquerypair (Key, value ){
If (object. isundefined (value) Return key;
Return key + '=' + encodeuricomponent (string. interpret (value ));
}
Function toquerystring (){
Return this. Inject ([], function (results, pair ){
Var key = encodeuricomponent (pair. Key), values = pair. value;
If (Values & typeof values = 'object '){
If (object. isarray (values ))
Return results. Concat (values. Map (toquerypair. Curry (key )));
} Else results. Push (toquerypair (Key, values ));
Return results;
}). Join ('&');
}
Function inspect (){
Return '# Return pair. Map (object. Inspect). Join (':');
}). Join (',') + '}> ';
}
Function tojson (){
Return object. tojson (this. toobject ());
}
Function clone (){
Return new hash (this );
}
Return {
Initialize: Initialize,
_ Each: _ each,
Set: set,
Get: get,
Unset: unset,
Toobject: toobject,
Totemplatereplacements: toobject,
Keys: keys,
Values: values,
Index: Index,
Merge: merge,
Update: update,
Toquerystring: toquerystring,
Inspect: inspect,
Tojson: tojson,
Clone: Clone
};
})());
Hash. From = $ h;
Clone
Each
Get
Inspect
Keys
Merge
Remove
Set
Tojson
Toobject
Toquerystring
Unset
Update
Value
Some examples of methods are provided, and the simple method is skipped.
Toquerystring (): Copy codeThe Code is as follows: $ H ({Action: 'ship ', order_id: 123, fees: ['f1', 'F2'], 'label': 'A demo '}). toquerystring ()
//-> 'Action = Ship & order_id = 123 & fees = F1 & fees = F2 & label = A % 20demo'
// An empty hash is an empty query string:
$ H (). toquerystring ()
//->''
Update (): Copy codeThe Code is as follows: var H = $ H ({Name: 'prototype', version: 1.5 });
H. Update ({version: 1.6, Author: 'Sam '}). Inspect ();
//-> # <Hash: {'name': 'prototype', 'version': 1.6, 'author': 'Sam '}>
H. Inspect ();
//-> # <Hash: {'name': 'prototype', 'version': 1.6, 'author': 'Sam '}>
// Note that the original hash object will be changed here
Merge (): Copy codeThe Code is as follows: var H = $ H ({Name: 'prototype', version: 1.5 });
H. Merge ({version: 1.6, Author: 'Sam '}). Inspect ();
//-> # <Hash: {'name': 'prototype', 'version': 1.6, 'author': 'Sam '}>
H. Inspect ();
//-> # <Hash: {'name': 'prototype', 'version': 1.5}>
// Note that the original hash object is not changed here