Created
August 3, 2016 04:59
-
-
Save ryanmorr/9bee0cf46dd8c4d587f55caac5c4bb1f to your computer and use it in GitHub Desktop.
A superior alternative to object literals for basic hash maps
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
function hashmap(...props) { | |
const map = Object.create(null, { | |
[Symbol.iterator]: { | |
enumerable: false, | |
writable: false, | |
configurable: false, | |
value: () => ({ | |
items: Object.entries(map), | |
next: function next() { | |
return { | |
done: this.items.length === 0, | |
value: this.items.shift() | |
}; | |
} | |
}) | |
} | |
}); | |
if (props.length) { | |
Object.assign(map, ...props); | |
} | |
return map; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment