复制代码代码如下:
/**
*
* 描述: JS 实现的 Mapa 方法
* @returns {map}
*/
função map () {
var struct = function (chave, valor) {
this.Key = key;
this.value = value;
};
// 添加 mapa 键值对
var put = function (chave, valor) {
for (var i = 0; i <this.arr.length; i ++) {
if (this.arr [i] .Key === key) {
this.arr [i] .Value = value;
retornar;
}
};
this.arr [this.arr.length] = new struct (chave, valor);
};
// 根据 根据 获取 Valor
var get = function (key) {
for (var i = 0; i <this.arr.length; i ++) {
if (this.arr [i] .Key === key) {
devolver este.arr [i] .Value;
}
}
retornar nulo;
};
// 根据 根据 删除 删除
var remover = function (chave) {
var V;
for (var i = 0; i <this.arr.length; i ++) {
v = this.arr.pop ();
if (v.Key === key) {
continuar;
}
this.arr.UnShift (v);
}
};
// 获取 mapa 键值对个数
var size = function () {
retornar este.arr.length;
};
// 判断 mapa 是否为空
var isEmpty = function () {
retornar this.arr.length <= 0;
};
this.arr = new Array ();
this.get = get;
this.put = put;
this.Remove = remover;
this.size = size;
this.isempty = isEmpty;
}
使用方法和 Java 中 mapa 类同
复制代码代码如下:
<script type = "text/javascript">
var map = new map ();
map.put ("num", 1);
map.put ("SS", "WSS");
alerta (map.size ());
alerta (map.get ("num"));
paraarr.remove ("num");
alerta (map.size ());
alerta (map.get ("num"));
</script>