本程序用JS寫(xiě)的一個(gè)類擬于JAVA中MAP類,可以對(duì)鍵值對(duì)進(jìn)行維護(hù).


          /*
          name:??? Map.js
          author:? WindDC
          date:??? 2006-10-27
          content: 本程序用JS實(shí)現(xiàn)類擬JAVA中MAP對(duì)像的功能
          */

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

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

          Map.prototype.put=function(key,value){//往容器中加入一個(gè)鍵值對(duì)
          ??????? for(var i=0;i<this.nodes.length;i++)
          ?????????? if(this.nodes[i].key==key){//如果鍵值已存在,則put方法為更新已有數(shù)據(jù)
          ?????????????? 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(){//獲取容器中對(duì)象的個(gè)數(shù)
          ??? ?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//刪除的是第一個(gè)元素
          ??? ?????? ? 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 閱讀(1485) 評(píng)論(6)  編輯  收藏 所屬分類: js/ajax
          Comments
          • # re: 用JS實(shí)現(xiàn)的MAP類
            BeanSoft
            Posted @ 2006-12-05 13:24
            贊一個(gè)!  回復(fù)  更多評(píng)論   
          • # re: 用JS實(shí)現(xiàn)的MAP類
            胡曉光
            Posted @ 2008-11-04 13:46
            Map 里面竟然是個(gè)Array,似乎不太好,直接用原型做個(gè)Map不就行了么,而且刪除值的時(shí)候還要循環(huán)遍歷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("未定義處理函數(shù)");
            for(var p in this.container){
            var ov = this.container[p];
            fun(ov);
            }
            }catch(e){
            return e;
            }
            };  回復(fù)  更多評(píng)論   
          • # re: 用JS實(shí)現(xiàn)的MAP類
            飛影
            Posted @ 2009-01-14 11:41
            發(fā)現(xiàn)這份代碼被拷貝了好多次啊。
            錯(cuò)也是一樣的拷貝,不知道樓上的有沒(méi)有仔細(xì)看過(guò)啊?

            if(this.length === 0) 三個(gè)等號(hào)。。。。  回復(fù)  更多評(píng)論   
          • # 一樓的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{//刪除的是第一個(gè)元素
            var nodes1=this.nodes.slice(1);
            }
            this.nodes=nodes1;
            break;
            }
            }//remove  回復(fù)  更多評(píng)論   
          • # 剛才說(shuō)錯(cuò)了,應(yīng)該是樓主的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{//刪除的是第一個(gè)元素
            var nodes1=this.nodes.slice(1);
            }
            this.nodes=nodes1;
            break;
            }
            }//remove  回復(fù)  更多評(píng)論   
          • # ...
            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{//刪除的是第一個(gè)元素
            var nodes1=this.nodes.slice(1);
            }
            this.nodes=nodes1;
            break;
            }
            }//remove  回復(fù)  更多評(píng)論   

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


          網(wǎng)站導(dǎo)航:
           
           
          主站蜘蛛池模板: 江门市| 轮台县| 富源县| 容城县| 苏尼特右旗| 盐边县| 洛宁县| 和林格尔县| 柘城县| 普洱| 德江县| 平陆县| 和龙市| 城步| 原阳县| 河曲县| 阳城县| 鹤壁市| 海林市| 建德市| 蒙阴县| 全椒县| 长汀县| 江油市| 永嘉县| 乌兰察布市| 甘南县| 西贡区| 会宁县| 天等县| 龙州县| 台中市| 金川县| 深州市| 雅江县| 琼结县| 克东县| 郓城县| 澎湖县| 巴南区| 台湾省|