JSFiddle - React, Tailwind, and code Playground
JavaScript
function HashMap() {}
HashMap.prototype.put = function(key, value) {
this[key] = value;
};
HashMap.prototype.get = function(key) {
if(typeof this[key] == 'undefined') {
throw new ReferenceError("key is undefined");
}
return this[key];
};
HashMap.prototype.size = function() {
var count = 0;
for(var prop in this) {
if(this.hasOwnProperty(prop)) {
count++;
}
}
return count;
};
// test
var map = new HashMap();
map.put("a", 1);
map.put("b", 2);
alert(map.size()); // 2