Created
October 24, 2012 04:34
-
-
Save island205/3943864 to your computer and use it in GitHub Desktop.
ES6-Map
This file contains hidden or 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
| #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