隨筆-9  評論-0  文章-0  trackbacks-0
          頁面上經常需要臨時保存一些數據,這些數據需要根據Key來保存或者修改Value,查看了些資料,稍微修改了一下。
          如下:
          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.update=function (key,value){
                  
          if(typeof(key)!="undefined"){
                      
          if(this.contains(key)==true){
                          
          this.remove(key);
                          
          this.add(key,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.indexValue =function(index){
                  
          var i=0;
                  
          for(var k in this._hash)
                  
          {
                      
          if(i==index)
                      
          {
                          
          return this._hash[k];
                      }

                      i
          ++;
                  }

              }

              
              
          ///返回值、根據KEY值來返回
              this.items = function(key){return this._hash[key];}
              
              
          ///是否存在true or false;
              this.contains = function(key)
                  
          return typeof(this._hash[key])!="undefined";
              }

              
          ///清空
              this.clear = function(){
                  
          for(var k in this._hash)
                  
          {
                      
          delete this._hash[k];
                  }

              }

          }


          使用方法:
          1、聲明
          var hashTab=new HashTable();
          2、添加Key和Value
          hashTab.add(strKey,strValue)
          3、修改
          hashTab.update(strKey,strValue);
          4、判斷Key是否存在
          hashTab.contains(strKey);
          5、刪除Key
          hashTab.remove(strKey)
          6、根據Key返回Value
          hashTab.items(strKey)
          7、返回記錄條數
          hashTab.count()
          8、根據IndexId返回第幾條數據(遍歷的時用)
          hashTab.indexValue(IndexId)
          9、清空所有數據
          hashTab.clear()
          posted on 2009-10-30 12:34 AndyFish 閱讀(334) 評論(0)  編輯  收藏 所屬分類: JavaScript

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 井冈山市| 定陶县| 勃利县| 象山县| 鄂尔多斯市| 崇仁县| 长武县| 石台县| 德州市| 富源县| 襄垣县| 鄢陵县| 峡江县| 石渠县| 桃园市| 治多县| 区。| 塔城市| 泽州县| 山东| 昌宁县| 荣成市| 莱西市| 彩票| 清涧县| 于都县| 朝阳区| 平果县| 交口县| 博罗县| 吴旗县| 宜阳县| 丁青县| 河津市| 山东省| 鄂温| 西乌| 微博| 集安市| 石狮市| 高密市|