Fixed the hashtableobj. set ("length", "0") bug.
You can set the key to ignore the case sensitivity.
Clone hashtable objects
You can use obj. valueOf ("key", "defalutvalue") to set the default value.
Bug fixes
Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
// Authors Birdshome, pouch @ phito, and Peng Haitao
Object. prototype. Clone = function ()
{
Var objClone;
If (this. constructor = Object) objClone = new this. constructor ();
Else objClone = new this. constructor (this. valueOf ());
For (var key in this)
{
If (objClone [key]! = This [key])
{
If (typeof (this [key]) = 'object ')
{
ObjClone [key] = this [key]. Clone ();
}
Else
{
ObjClone [key] = this [key];
}
}
}
ObjClone. toString = this. toString;
ObjClone. valueOf = this. valueOf;
Return objClone;
}
Function Hashtable (){
This. clear = hashtable_clear;
This. containsKey = hashtable_containsKey;
This. containsValue = hashtable_containsValue;
This. get = hashtable_get;
This. isEmpty = hashtable_isEmpty;
This. keys = hashtable_keys;
This. put = hashtable_put;
This. remove = hashtable_remove;
This. size = hashtable_size;
This. toString = hashtable_toString;
This. values = hashtable_values;
This. hashtable = new Object ();
This. set = hashtable_set;
This. valueOf = hashtable_valueOf;
This. clone = hashtable_clone;
This. ignoreupperlower = true;
// Ignore case sensitivity
}
/* ======= Private methods for internal use only ========== */
Function hashtable_clone (){
Return this. Clone ();
}
Function hashtable_put (key, value ){
If (this. ignoreupperlower & typeof (key) = "string "){
Key = key. toUpperCase ();
}
If (key = null | value = null ){
Throw "NullPointerException {" + key + "},{" + value + "}";
} Else {
This. hashtable [key] = value;
}
}
Function hashtable_set (key, value ){
If (this. ignoreupperlower & typeof (key) = "string "){
Key = key. toUpperCase ();
}
If (this. containsKey (key )){
This. remove (key );
}
This. put (key, value );
}
Function hashtable_get (key ){
If (this. ignoreupperlower & typeof (key) = "string "){
Key = key. toUpperCase ();
}
Return this. hashtable [key];
}
Function hashtable_valueOf (key, defvalue ){
Var ret = this. get (key );
If (typeof (ret) = "undefined "){
Return defvalue;
}
Return ret;
}
Function hashtable_remove (key ){
If (this. containsKey (key )){
Delete this. hashtable [key];
}
}
Function hashtable_isEmpty (){
Return (parseInt (this. size () = 0 )? True: false;
}
Function hashtable_size (){
Var size = 0;
For (var I in this. hashtable ){
If (typeof (this. hashtable [I]) = "function "){
Continue;
}
If (this. hashtable [I]! = Null ){
Size ++;
}
}
Return size;
}
Function hashtable_toString (){
Var result = "";
For (var I in this. hashtable ){
If (typeof (this. hashtable [I]) = "function "){
Continue;
}
If (this. hashtable [I]! = Null ){
Result + = "{" + I + ":" + this. hashtable [I] + "} \ n ";
}
}
Return result;
}
Function hashtable_clear (){
This. hashtable = new Object ();
}
Function hashtable_containsKey (key ){
If (this. ignoreupperlower & typeof (key) = "string "){
Key = key. toUpperCase ();
}
Var exists = false;
For (var I in this. hashtable ){
If (typeof (this. hashtable [I]) = "function "){
Continue;
}
If (I = key & this. hashtable [I]! = Null ){
Exists = true;
Break;
}
}
Return exists;
}
Function hashtable_containsValue (value ){
Var contains = false;
If (value! = Null ){
For (var I in this. hashtable ){
If (typeof (this. hashtable [I]) = "function "){
Continue;
}
If (this. hashtable [I] = value ){
Contains = true;
Break;
}
}
}
Return contains;
}
Function hashtable_values (){
Var values = new Object ();
For (var I in this. hashtable ){
If (typeof (this. hashtable [I]) = "function "){
Continue;
}
If (this. hashtable [I]! = Null) values. push (this. hashtable [I]);
}
Return values;
}
Function hashtable_keys (){
Var keys = new Object ();
For (var I in this. hashtable ){
If (typeof (this. hashtable [I]) = "function "){
Continue;
}
Keys. push (I );
}
Return keys;
}
Function test (){
Var ht = new Hashtable ();
Ht. put ("3", "Jackson ");
Ht. put ("2", "Tom ");
Ht. put ("4", 3 );
Ht. set ("length", 445555 );
Ht. set ("ddd", "ddd ");
Ht. set ("index", "ddd ");
Var et = ht. toString ();
Ht. ignoreupperlower = false;
// Case insensitive
Ht. clear ();
Ht. put ("3", "Jackson ");
Ht. put ("2", "Tom ");
Ht. remove ("2 ");
Ht. put ("4", 3 );
Ht. set ("length", 5 );
// If new Array is used, the length of the Array is set.
Ht. set ("index", "ddd ");
Ht. set ("ddd", "ddd ");
Alert (et + "" + ht. toString () + "" + ht. size ());
Var cloneobj = ht. clone ();
Alert (cloneobj. toString ());
}
</Script>
</Head>
<Body onload = "test ()">
</Body>
</Html>
If you want to use hashtable and hashset with better functionality Please download: http://xiazai.jb51.net/201012/yuanma/jshashtable.rar