Created
October 27, 2012 17:22
-
-
Save soundstep/3965388 to your computer and use it in GitHub Desktop.
javascript hash map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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