zoukankan      html  css  js  c++  java
  • 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());

  • 相关阅读:
    数组中删除指定某个元素(根据值删除,不是位置)
    gulp使用过程中走过的坑
    H5兼容不同屏幕尺寸
    jQuery基础——事件
    DOM的jquery操作(遍历)
    jquery的DOM操作(创建节点、插入节点、删除节点、复制节点、替换节点、包裹节点)
    gulp插件uncss的使用
    【代码总结-不定期更新】
    【Linux-学习笔记-不定期更新】
    【随时记录的一些东东-不定期更新】
  • 原文地址:https://www.cnblogs.com/fqs123456/p/3081502.html
Copyright © 2011-2022 走看看