Skip to content

Instantly share code, notes, and snippets.

@island205
Created October 24, 2012 04:34
Show Gist options
  • Select an option

  • Save island205/3943864 to your computer and use it in GitHub Desktop.

Select an option

Save island205/3943864 to your computer and use it in GitHub Desktop.
ES6-Map
#https://developer.mozilla.org/en-US/docs/JavaScript/ECMAScript_6_support_in_Mozilla
Map = Map || do ->
id = 0
keyToObj = {}
keyToValue = {}
unique = ->
"Map_#{id++}"
getKeyByObj = (obj)->
for own key, value of keyToObj
if obj is value
foundKey = key
foundKey
class Map
constructor:->
get:(obj)->
foundKey = getKeyByObj obj
if not not foundKey
keyToValue[foundKey]
set:(obj, value)->
foundKey = getKeyByObj obj
if not not obj
foundKey = unique() unless foundKey
keyToObj[foundKey] = obj
keyToValue[foundKey] = value
return
delete:(obj)->
foundKey = getKeyByObj obj
if not not foundKey
delete keyToObj[foundKey]
delete keyToValue[foundKey]
size:->
size = 0
for own key, value of keyToObj
if not not value
size++
size
Map
map = new Map()
console.log map
console.log map.get document
map.set document, "document"
console.log map.get document
console.log map.size()
map.set document, "doc"
console.log map.get document
console.log map.size()
debugger
map.delete document
console.log map.get document
console.log map.size()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment