大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 网页设计 -> DREAMWEAVER -> 用 Javascript 写的一个映射表类

用 Javascript 写的一个映射表类

时间: 2021-07-31 作者:daque

该类不妨经过要害字(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 十足采用 提醒:你可先窜改局部代码,再按运转]

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.