分享

博客园 - DooIT - js写的Hashtable类

 顾祥先 2005-10-04
 
<script language="javascript" type="text/javascript">

function Hashtable()
{
    
this._hash        = new Object();
    
this.add        = function(key,value){
                        
if(typeof(key)!="undefined"){
                            
if(this.contains(key)==false){
                                
this._hash[key]=typeof(value)=="undefined"?null:value;
                                
return true;
                            } 
else {
                                
return false;
                            }
                        } 
else {
                            
return false;
                        }
                    }
    
this.remove        = function(key){delete this._hash[key];}
    
this.count        = function(){var i=0;for(var k in this._hash){i++;} return i;}
    
this.items        = function(key){return this._hash[key];}
    
this.contains    = function(key){ return typeof(this._hash[key])!="undefined";}
    
this.clear        = function(){for(var k in this._hash){delete this._hash[k];}}

}

var a = new Hashtable();

a.add(
"aa");
a.add(
"bb",2342);
a.add(
"bb",2342);

a.remove(
"aa");

alert(a.count());

alert(a.contains(
"bb"));

alert(a.contains(
"aa"));

alert(a.items(
"bb"));


</script>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约