Great article! An important thing to keep in mind when using objects as lookup tables is that objects have prototype chains that will also be checked if you are not careful. Look at this example, using some code from the article:
There are a few fixes for this:
Create the object without a prototype (Object.create(null)):
constpokemon=Object.assign(Object.create(null),{Water:'Squirtle',Fire:'Charmander',Plant:'Bulbasur',Electric:'Pikachu'});// pokemon.toString does not exist!
Only check the object's "own" properties (hasOwnProperty)
Great article! An important thing to keep in mind when using objects as lookup tables is that objects have prototype chains that will also be checked if you are not careful. Look at this example, using some code from the article:
There are a few fixes for this:
Create the object without a prototype (
Object.create(null)):Only check the object's "own" properties (
hasOwnProperty)As you also suggested: Use a
Mapwhich inherently doesn't have this problem.Thanks for the content!