分享

js map 使用

 moonboat 2009-03-18
<script>

function struct(key, value) {
  this.key = key;
  this.value = value;
}
function MapClass(){
 this.map = new Array();
 this.set=function(key, value) {
    for (var i = 0; i < this.map.length; i++)
    {
   if ( this.map[i].key === key )
   {
     this.map[i].value = value;
     return;
   }
    } 
  this.map[this.map.length] = new struct(key, value);
 },
  this.get=function(key)
  {
    for (var i = 0; i < this.map.length; i++)
     {
   if ( this.map[i].key === key )
    {
     return this.map[i].value;
   }
    }
   
    return null;
  },
  this.removeKey=function(key)
  {
    var v;
    for (var i = 0; i < this.map.length; i++)
     {
   v = this.map.pop();
   if ( v.key === key )
     continue;
    
   this.map.unshift(v);
    }
  },
  this.getCount=function() {
    return this.map.length;
  },
  this.isEmpty=function() {
    return this.map.length <= 0;
  }
}
var c = new MapClass();
var d = new MapClass();
c.set("xx","yy");
d.set("xx","ddd");
alert(c.get("xx"));
alert(d.get("xx"));
</script>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多