隨筆-9  評論-0  文章-0  trackbacks-0
          頁面上經(jīng)常需要臨時(shí)保存一些數(shù)據(jù),這些數(shù)據(jù)需要根據(jù)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];}
              
          ///記錄條數(shù)
              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
          ++;
                  }

              }

              
              
          ///返回值、根據(jù)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、根據(jù)Key返回Value
          hashTab.items(strKey)
          7、返回記錄條數(shù)
          hashTab.count()
          8、根據(jù)IndexId返回第幾條數(shù)據(jù)(遍歷的時(shí)用)
          hashTab.indexValue(IndexId)
          9、清空所有數(shù)據(jù)
          hashTab.clear()
          posted on 2009-10-30 12:34 AndyFish 閱讀(332) 評論(0)  編輯  收藏 所屬分類: JavaScript

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 曲沃县| 镇坪县| 乐至县| 碌曲县| 昂仁县| 绥中县| 民权县| 全州县| 航空| 淮北市| 德格县| 扎兰屯市| 蓝山县| 沂水县| 肥城市| 海盐县| 乌兰察布市| 广汉市| 枣阳市| 天镇县| 阿图什市| 华池县| 广东省| 崇文区| 安陆市| 武清区| 栾川县| 青龙| 九江县| 札达县| 乌兰浩特市| 台江县| 黔南| 蛟河市| 汤阴县| 祁东县| 黄平县| 嘉黎县| 个旧市| 永胜县| 韶山市|