Skip to content

Instantly share code, notes, and snippets.

@soundstep
Created October 27, 2012 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soundstep/3965388 to your computer and use it in GitHub Desktop.
Save soundstep/3965388 to your computer and use it in GitHub Desktop.
javascript hash map
var HashMap = function(){
var uuid = function(a,b){for(b=a='';a++<36;b+=a*51&52?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-');return b;}
var getKey = function(target) {
if (!target) return;
if (typeof target !== 'object') return target;
return target.hashkey ? target.hashkey : target.hashkey = uuid();
}
return {
put: function(key, value) {
this[getKey(key)] = value;
},
get: function(key) {
return this[getKey(key)];
},
remove: function(key) {
var k = getKey(key);
this[k] = null;
delete this[k];
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment