What is the preferred syntax for defining enums in JavaScript? Something like:
my.namespace.ColorEnum = {
RED : 0,
GREEN : 1,
BLUE : 2
}
// later on
if(currentColor == my.namespace.ColorEnum.RED) {
// whatever
}
Or is there a more preferable idiom?
Even though only static methods (and not static properties) are supported in ES2015 (see here as well, §15.2.2.2), curiously you can use the below with Babel with the
es2015
preset:I found this to be working as expected even across modules (e.g. importing the
CellState
enum from another module) and also when I import a module using Webpack.The advantage this method has over most other answers is that you can use it alongside a static type checker (e.g. Flow) and you can assert, at development time using static type checking, that your variables, parameters, etc. are of the specific
CellState
"enum" rather than some other enum (which would be impossible to distinguish if you used generic objects or symbols).update
The above code has a deficiency in that it allows one to create additional objects of type
CellState
(even though one can't assign them to the static fields ofCellState
since it's frozen). Still, the below more refined code offers the following advantages:CellState
may be createdthe
values
function that returns all instances of the enum does not have to create the return value in the above, manual (and error-prone) way.You can do something like this
As defined in this library. https://github.com/webmodule/foo/blob/master/foo.js#L217
Complete example https://gist.github.com/lnt/bb13a2fd63cdb8bce85fd62965a20026
This is the solution that I use.
And you define your enums like this:
And this is how you access your enums:
I usually use the last 2 methods for mapping enums from message objects.
Some advantages to this approach:
Some disadvantages:
If you're using Backbone, you can get full-blown enum functionality (find by id, name, custom members) for free using Backbone.Collection.
This is how Typescript translates it's
enum
into Javascript:Now:
At first I was confused why
obj[1]
returns'Active'
, but then realised that its dead simple - Assignment operator assigns value and then returns it:The "Perfered Syntax" by most people has already been listed above. However, there is a major overarching problem: performance. Not a single one of the above answers is very performant in the slightest and they all bloat your code size to the extreme. For real performance, ease of reading code, and an unprecedented reduction in code size by minification, this is the correct way to do enumerations.
Additionally, this syntax allows for clear and concise class extending as seen below.
(Length: 2,450 bytes)
Some may say that this is less practical than other solutions: it waists tons of space, it takes a long time to write, and its not coated with sugar syntax. Yes, those people would be right if they do not minify their code. However, no reasonable person would leave unminified code in the end product. For this minification, Closure Compiler is the best I have yet to find. Online access can be found here. Closure compiler is able to take all of this enumeration data and inline it, making your javascript run super duper fast and be super duper small. Observe.
(Length: 605 bytes)
Now, let us see how big the equivalent file would be without any of these enumerations.
Source Without These Enumerations (length: 1,973 bytes (477 bytes shorter!))
Minified Without These Enumerations (length: 843 bytes (238 bytes longer))
As seen, without enumerations, the source code is shorter at the cost of a larger minified code. I do not know about you, I know for sure that I don't like to incorporate source code into the end product, making this enumeration way far superior insomuch that it results in smaller file sizes. Add to that this form of enumeration being way faster. Indeed, this form of enumeration is the way to go.