var Map = function() {
this.entries = new Array();
var Entry = function(key, value) {
this.key = key;
this.value = value;
};
this.put = function(key, value) {
for ( var i = 0; i < this.entries.length; i++) {
if (this.entries[i].key === key) {
return false;
}
}
this.entries.push(new Entry(key, value));
return true;
};
this.get = function(key) {
for ( var i = 0; i < this.entries.length; i++) {
if (this.entries[i].key === key) {
return this.entries[i].value;
}
}
return null;
};
this.remove = function(key) {
var index = this.indexOf(key);
if (index != -1) {
this.entries.splice(index, 1);
}
};
this.setValue = function(key, value) {
var index = this.indexOf(key);
if (index != -1) {
this.entries[index].value = value;
}
};
this.getEntry = function(index) {
if (index >= 0 && index < this.size()) {
return this.entries[index];
}
return null;
};
this.size = function() {
return this.entries.length;
};
this.isEmpty = function() {
return this.entries.length <= 0;
};
this.clear = function() {
this.entries = [];
};
this.indexOf = function(key) {
var index = -1;
for ( var i = 0; i < this.size(); i++) {
if (key == this.entries[i].key) {
index = i;
break;
}
}
return index;
};
this.toString = function() {
var str = '[';
for ( var i = 0; i < this.size(); i++) {
str += (this.entries[i].key + '=' + this.entries[i].value + ',');
}
str = str.substr(0, str.length - 1);
str += ']';
return str;
};
};
js模拟Map
最新推荐文章于 2019-08-03 16:08:00 发布