<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    本程序用JS寫的一個類擬于JAVA中MAP類,可以對鍵值對進行維護.


    /*
    name:??? Map.js
    author:? WindDC
    date:??? 2006-10-27
    content: 本程序用JS實現類擬JAVA中MAP對像的功能
    */

    function Node(key,value){//鍵值對對象
    ??? this.key=key;
    ??? this.value=value;
    }

    function Map(){//Map類
    ??? this.nodes=new Array();
    }

    Map.prototype.put=function(key,value){//往容器中加入一個鍵值對
    ??????? for(var i=0;i<this.nodes.length;i++)
    ?????????? if(this.nodes[i].key==key){//如果鍵值已存在,則put方法為更新已有數據
    ?????????????? this.nodes[i].value=value;
    ?????????????? return;
    ?????????? }
    ??????? var node=new Node(key,value);
    ??????? this.nodes.push(node);
    ??????? return;
    }//put

    ??
    Map.prototype.get=function(key){//獲取指定鍵的值
    ??????? for(var i=0;i<this.nodes.length;i++)
    ?????????? if(this.nodes[i].key==key)
    ????????????? return this.nodes[i].value;
    ??????? return null;
    }//get
    ????
    Map.prototype.size=function(){//獲取容器中對象的個數
    ??? ?return this.nodes.length;
    }//size

    ??? ????
    Map.prototype.clear=function(){//清空容器
    ??? ?while(this.nodes.length>0)
    ??? ??? this.nodes.pop();?????
    }//clear
    ?
    Map.prototype.remove=function(key){//刪除指定值
    ??? ?for(var i=0;i<this.nodes.length;i++)
    ??? ??? if(this.nodes[i].key==key){
    ??? ??? ?? if(i>0)
    ??? ????????? var nodes1=this.nodes.concat(this.nodes.slice(0,i-1),this.nodes.slice(i+1));
    ??? ?????? else//刪除的是第一個元素
    ??? ?????? ? var nodes1=nodes.slice(1);
    ??? ?????? this.nodes=nodes1;

    ??? ??? }
    }//remove

    ???
    Map.prototype.isEmpty=function(){//是否為空
    ??? ?if(this.nodes.length==0)
    ??? ?? return true;
    ??? ?else
    ??? ?? return false;
    }//isEmpty
    ???
    Map.prototype.toString=function(){
    ???? var str="[";
    ???? for(var i=0;i<this.nodes.length;i++){
    ??????? if(i<this.nodes.length-1)
    ?????????? str=str+this.nodes[i].key+",";
    ?????? else
    ?????????? str=str+this.nodes[i].key;????
    ?? ?}
    ??? str=str+"]";
    ????return str;
    }

    posted on 2006-12-01 17:24 WindDC 閱讀(1480) 評論(6)  編輯  收藏 所屬分類: js/ajax
    Comments
    • # re: 用JS實現的MAP類
      BeanSoft
      Posted @ 2006-12-05 13:24
      贊一個!  回復  更多評論   
    • # re: 用JS實現的MAP類
      胡曉光
      Posted @ 2008-11-04 13:46
      Map 里面竟然是個Array,似乎不太好,直接用原型做個Map不就行了么,而且刪除值的時候還要循環遍歷Array
      貼出我的方案:
      function HashMap(){
      this.length = 0;
      this.container = {};
      }

      HashMap.prototype.put = function(objName,objValue){
      try{
      if(objValue && objName && objName != ""){
      this.container[objName] = objValue;
      this.length ++ ;
      }
      }catch(e){
      return e;
      }
      };

      HashMap.prototype.get = function(objName){
      try{
      if(this.container[objName])
      return this.container[objName];
      }catch(e){
      return e;
      }
      };

      HashMap.prototype.contain = function(objValue){
      try{
      for(var p in this.container){
      if(this.container[p] === objValue)
      return true;
      }
      return false;
      }catch(e){
      return e;
      }
      };

      HashMap.prototype.remove = function(objName){
      try{
      if(this.container[objName]){
      delete this.container[objName];
      this.length -- ;
      }
      }catch(e){
      return e;
      }
      };

      HashMap.prototype.pop = function(objName){
      try{
      var ov = this.container[objName];
      if(ov){
      delete this.container[objName];
      this.length -- ;
      return ov;
      }
      return null;
      }catch(e){
      return e;
      }
      };

      HashMap.prototype.removeAll = function(){
      try{
      this.clear();
      }catch(e){
      return e;
      }
      };

      HashMap.prototype.clear = function(){
      try{
      delete this.container;
      this.container = {};
      this.length = 0;
      }catch(e){
      return e;
      }
      };

      HashMap.prototype.isEmpty = function(){
      if(this.length === 0)
      return true;
      else
      return false;
      };

      HashMap.prototype.runIn = function(fun){
      try{
      if(!fun)
      throw new Error("未定義處理函數");
      for(var p in this.container){
      var ov = this.container[p];
      fun(ov);
      }
      }catch(e){
      return e;
      }
      };  回復  更多評論   
    • # re: 用JS實現的MAP類
      飛影
      Posted @ 2009-01-14 11:41
      發現這份代碼被拷貝了好多次啊。
      錯也是一樣的拷貝,不知道樓上的有沒有仔細看過啊?

      if(this.length === 0) 三個等號。。。。  回復  更多評論   
    • # 一樓的remove方法有誤,正確的如下
      Xwei
      Posted @ 2009-02-20 16:28
      Map.prototype.remove=function(key){//刪除指定值
      for(var i=0;i<this.nodes.length;i++)
      if(this.nodes[i].key==key){
      if(i>0){
      var nodes1=(this.nodes.slice(0,i)).concat(this.nodes.slice(i+1,this.nodes.length));
      }else{//刪除的是第一個元素
      var nodes1=this.nodes.slice(1);
      }
      this.nodes=nodes1;
      break;
      }
      }//remove  回復  更多評論   
    • # 剛才說錯了,應該是樓主的remove方法有誤,正確的如下
      Xwei
      Posted @ 2009-02-20 16:29
      Map.prototype.remove1=function(key){//刪除指定值
      for(var i=0;i<this.nodes.length;i++)
      if(this.nodes[i].key==key){
      if(i>0){
      var nodes1=(this.nodes.slice(0,i)).concat(this.nodes.slice(i+1,this.nodes.length));
      }else{//刪除的是第一個元素
      var nodes1=this.nodes.slice(1);
      }
      this.nodes=nodes1;
      break;
      }
      }//remove  回復  更多評論   
    • # ...
      Xwei
      Posted @ 2009-02-20 16:30
      Map.prototype.remove=function(key){//刪除指定值
      for(var i=0;i<this.nodes.length;i++)
      if(this.nodes[i].key==key){
      if(i>0){
      var nodes1=(this.nodes.slice(0,i)).concat(this.nodes.slice(i+1,this.nodes.length));
      }else{//刪除的是第一個元素
      var nodes1=this.nodes.slice(1);
      }
      this.nodes=nodes1;
      break;
      }
      }//remove  回復  更多評論   

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


    網站導航:
     
     
    主站蜘蛛池模板: 亚洲伊人久久大香线蕉| 久久亚洲精品国产精品黑人| 国产精品亚洲精品青青青| 免费看又黄又无码的网站| 亚洲国产美国国产综合一区二区 | 亚洲男人天堂影院| 69视频在线观看高清免费| 亚洲精品美女久久久久9999| 91福利视频免费| 亚洲一区在线视频| 四虎影视大全免费入口| 女bbbbxxxx另类亚洲| jlzzjlzz亚洲乱熟在线播放| 中文字字幕在线高清免费电影| 亚洲国产a∨无码中文777| 18以下岁毛片在免费播放| 亚洲色精品VR一区区三区| 拔擦拔擦8x华人免费久久| 一区二区3区免费视频| 亚洲国产成人片在线观看无码 | 蜜桃传媒一区二区亚洲AV| 日批日出水久久亚洲精品tv| 中文字幕手机在线免费看电影| 国产成人亚洲精品青草天美| 114一级毛片免费| 色欲aⅴ亚洲情无码AV| 亚洲香蕉网久久综合影视| 最近免费mv在线电影| 亚洲av无码专区在线电影| 亚洲成AV人在线观看天堂无码| A在线观看免费网站大全| 在线观看亚洲精品专区| 亚洲AV日韩AV永久无码免下载| 一二三四视频在线观看中文版免费 | 阿v免费在线观看| 精品亚洲成a人片在线观看少妇| 免费黄色网址入口| 久久99毛片免费观看不卡| 久久久久亚洲国产| 久久精品国产亚洲一区二区| 好爽…又高潮了毛片免费看|