邮件订阅
5ucms仿站,用Javascript写的一个映射表类
作者: 来源: 日期:2015/11/14 5:39:16 浏览量:44(滞后) 人气:LOADING...(实时) 【

5ucms仿站,用Javascript写的一个映射表类

该类可以通过关键字(key)查找相对应的值(value),关键字的类型可以是String、Number、Boolean类型,值的类型不限,代码如下:

<script>
function struct(key, value){

  this.key = key;
  this.value = http://www.dedecms.com/web-art/htmlbase/Javascript/20050506/value;

}

function setAt(key, value){
 
  for (var i = 0; i < this.map.length; i++)
  {
    if ( this.map[i].key === key )
    {
      this.map[i].value = http://www.dedecms.com/web-art/htmlbase/Javascript/20050506/value;
      return;
    }
  }
 
  this.map[this.map.length] = new struct(key, value);

}

function lookUp(key)
{
  for (var i = 0; i < this.map.length; i++)
  {
    if ( this.map[i].key === key )
    {
      return this.map[i].value;
    }
  }
 
  return null;
}

function removeKey(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);
  }
}

function getCount(){
  return this.map.length;
}

function isEmpty(){
  return this.map.length <= 0;
}

function classMap() {

  this.map = new Array();

  this.lookUp = lookUp;
  this.setAt = setAt;
  this.removeKey = removeKey;
  this.getCount = getCount;
  this.isEmpty = isEmpty;
}

window.onload = function(){

  var map = new classMap();

  alert("is the map empty? " + map.isEmpty());
 
  // string to array
  map.setAt("sw1", new Array("sw1_1"));
  map.setAt("sw2", new Array("sw2_1", "sw2_2"));
  map.setAt("sw3", new Array("sw3_1", "sw3_2", "sw3_3"));
  alert(map.lookUp("sw5")); // null
  alert(map.lookUp("sw2")); // "sw2_1, sw2_2"
 
  alert(map.getCount()); // 3
 
  // number to string
  map.setAt(1, "sw1");
  map.setAt(2, "sw2");
  alert(map.lookUp(2)); // "sw2"
  map.setAt(2, new Array("sw2_1", "sw2_2"));
  alert(map.lookUp(2)); // "sw2_1, sw2_2"
 
  alert(map.getCount()); // 5
 
  // string to number
  map.setAt("1", 1);
  map.setAt("2", 2);
  alert(map.lookUp("1")); // 1
  alert(map.lookUp(1)); // "sw1"
  map.setAt("sw3", 33);
  alert(map.lookUp("sw3")); // 33
 
  alert(map.getCount()); // 7
 
  // number to number
  map.setAt(1, 11);
  map.setAt(2, 22);
  alert(map.lookUp(1)); // 11
 
  alert(map.getCount()); // 7
 
  map.removeKey(1);
  alert(map.lookUp(1)); // null
 
  alert(map.getCount()); // 6
 
  // boolean to array
  map.setAt(false, new Array("false", "true"));
  alert(map.lookUp(false));
 
  alert(map.getCount()); // 7

}
</script>


选择5ucms.org 选择未来

本站推荐: 5ucms模板下载 5ucms插件下载 仿站联系Q3876307       [复制给好友] [打印] [关闭] [返回] [顶部]
上一篇:5ucms培训,JavaScript通用库(一)
下一篇:5ucms插件,删除数组元素.
本站声明:本网站所载文章等内容,目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权及其它问题,请在30日内与本网联系(Email:3876307#qq.com),我们将在第一时间删除内容。若原创内容转载请注明出处。
5ucms仿站,用Javascript写的一个映射表类的关键词:
评论信息
相关分类
本周热门
本月热门
关于我们 - 版权/免责 申明 - 建站服务 - 网站地图 - 稿件投递 - 联系我们 - 5ucms
Copyright © 2008-2015 www.5ucms.org