梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
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());
posted on 2009-10-16 15:09 HUIKK 阅读(1873) 评论(1)  编辑  收藏 所属分类: JavaScript

评论

# re: js hashTable 2009-10-16 17:44 翔南
不错 收藏!  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: