Enums
ℹ️ Used as a collection of related values. Use enums when a method or variable can only take one out of a small set of possible values.
An enum is a way to organize a collection of related values.
These enums values are number-based:
Direction.Up
is 0
, Direction.Down
is 1
and so on...
Now, let's look at the JavaScript that it generates:
Let's take the line Seasons[Seasons["Summer"] = 0] = "Summer";
. To understand this line we just need to analyze Seasons["Summer"] = 0
, here we can see that it assigns value 0
to the Seasons.Summer
, then we know that assignment operator returns the assigned value, so further execution is Seasons[0] = "Summer"
.
Enums have autoincrementing behavior:
When Up
is initilized with 1
, others have auto-incremented value from that point. Direction.Down
has value 2
, Direction.Left
has 3
, Direction.Right
has 4
.
String Enums
String enums don't have auto-increment behavior. It's required to give assign a value to each enum member.
Generated code is the following:
So
Read More
Last updated