该类可以通过要害字(key)查找相对应的值(value),要害字的类型可以是String、Number、Boolean类型,值的类型不限,代码如下:
运行代码框<script> function struct(key, value){ this.key = key; this.value = value; } function setAt(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); } 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> [Ctrl A 全部选择 提示:你可先修改部分代码,再按运行]
怎样把网站变为灰色
DIV CSS 网页兼容全搞定 (IE6 IE7 IE8 IE9
IE浏览器兼容性另类解决方案
CSS布局口诀 ,快速学习css的顺口溜
介绍span和div标签的使用
入门:Dreamweaver8设计网页背景实例讲解
Web技术前沿:那些让人惊叹的 CSS3 应用演
表格中单元格之间分隔线的隐藏方法
在Dreamweaver中为插入的Flash添加透明
CSS有序列表OL的一些特殊形式
实例学习网页制作中常用的网页表单的应用
应用CSS的page-break-after属性 实现WEB页