Javascript Tutorial - Enum Types

preview_player
Показать описание
In reality Javascript does not support enumerated types, but what you can do is define a global constant object that looks and behaves like an Enum.

We look at how you can use this method to define an 'enum' that stores the values for directions that a player might be moving towards in a game.

There are many ways to do this so if you have any better ideas I'd love to hear your feedback in the comments!

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
Рекомендации по теме
Комментарии
Автор

generally when i define my enums i always use Object.freeze on them and use numbers as values just to make sure they really act like enums

rose
Автор

Thanks, man. I like using Enums b/c it can translate inputs to a word format, rather than using numbers, so I was disappointed to find that JavaScript doesn't have them. I'll keep this in mind for the future. 👍

AlexTechie
Автор

Enums seem like such a pointless structure in JS, you essentially created a hash map via an object literal... Enums are a fluffy idea that is totally pointless in languages like JS and Python.

collegeofbro
Автор

Except the values of that Object should be numbers hence the name ENUMERATION.

mrchedda
visit shbcf.ru