javascript实现HashTable

来源:互联网 发布:法国华人 知乎 编辑:程序博客网 时间:2024/06/17 01:13
function HashTable(){
this.hash = {};
this._count = 0;
this.set = function(key,value){
if (this._hash.hasOwnProperty(key)) {
this._hash[key] = value;
return false;
} else {
this._hash[key] = value;
this._count++;
return true;
}
}
this.remove = function(key) {
delete this._hash[key];
this._count--;
}
this.count = function() {
return this._count;
}
this.get = function(key) {
if (this.contains(key))
return this._hash[key];
}
this.contains = function(key) {
return this._hash.hasOwnProperty(key);
}
this.clear = function() {
this._hash = {};
this._count = 0;
}
}
原创粉丝点击