复制代码代码如下::
/**
*
* 描述 : JS 实现的 Karte 方法
* @returns {map}
*/
Funktion MAP () {
var struct = Funktion (Schlüssel, Wert) {
this.key = key;
this.Value = Wert;
};
// 添加 Karte 键值对
var put = Funktion (Schlüssel, Wert) {
für (var i = 0; i <this.arr.length; i ++) {
if (this.arr [i] .Key === Schlüssel) {
this.arr [i] .Value = Wert;
zurückkehren;
}
};
this.arr [this.arr.length] = new struct (Schlüssel, Wert);
};
// 根据 Schlüssel 获取 Wert
var get = function (Schlüssel) {
für (var i = 0; i <this.arr.length; i ++) {
if (this.arr [i] .Key === Schlüssel) {
kehre diesen.arr [i] .Value;
}
}
null zurückkehren;
};
// 根据 Schlüssel 删除
var remove = function (taste) {
var v;
für (var i = 0; i <this.arr.length; i ++) {
v = this.arr.pop ();
if (v.key === Schlüssel) {
weitermachen;
}
this.arr.unshift (v);
}
};
// 获取 Karte 键值对个数
var size = function () {
Gibt dies zurück.arr.length;
};
// 判断 Karte 是否为空
var isempty = function () {
return this.arr.length <= 0;
};
this.arr = new Array ();
this.get = GET;
this.put = put;
this.remove = entfernen;
this.size = Größe;
this.isempty = isempty;
}
使用方法和 Java 中 Karte 类同
复制代码代码如下::
<script type = "text/javaScript">
var map = new map ();
map.put ("num", 1);
map.put ("SS", "WSS");
alert (map.size ());
alert (map.get ("num"));
parAarr.remove ("num");
alert (map.size ());
alert (map.get ("num"));
</script>