`
hwpok
  • 浏览: 241984 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

js hashTable

 
阅读更多
function Hashtable() 
        
{
            
this.container = new Object();
            
            
/**//** put element */
            
this.put = function (key, value) 
            
{
                
if (typeof (key) == "undefined")
                
{
                    
return false;
                }
 
                
if (this.contains(key))
                
{
                    
return false;
                }
 
                
this.container[key] = typeof (value) == "undefined" ? null : value;
                
return true;
            }
;
        
            
/**//** remove element */
            
this.remove = function (key) 
            
{
                
delete this.container[key];
            }
;
            
            
/**//** get size */
            
this.size = function () 
            
{
                
var size = 0;
                
for (var attr in this.container) 
                
{
                    size
++;
                }

                
return size;
            }
;
            
            
/**//** get value by key */
            
this.get = function (key) 
            
{
                
return this.container[key];
            }
;
        
            
/**//** containts a key */
            
this.contains = function (key) 
            
{
                
return typeof (this.container[key]) != "undefined";
            }
;
        
            
/**//** clear all entrys */
            
this.clear = function () 
            
{
                
for (var attr in this.container)
                
{
                    
delete this.container[attr];
                }

            }
;
            
            
/**//** hashTable 2 string */
            
this.toString = function()
            
{
                
var str = "";
                
for (var attr in this.container)
                
{
                    str 
+= "," + attr + "=" + this.container[attr];
                }

                
if(str.length>0)
                
{
                    str 
= str.substr(1, str.length);
                }

                
return "{" + str + "}";
            }
;
        }

        
        
var hashtable = new Hashtable();
        hashtable.put('
1','huyvanpull');
        hashtable.put('
2','ensoodge');
        hashtable.put('
3','huyfan');
        
        hashtable.remove('
2');
        alert(hashtable.toString());
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics