In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you the content of sample analysis of enumerated types in TypeScript. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
First, what is it?
An enumeration is a collection of named integer constants used to declare a set of named constants, which can be defined as an enumeration type when a variable has several possible values
In popular terms, an enumeration is a collection of all possible values of an object
It is also common in daily life. For example, SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY indicating the day of the week can be regarded as an enumeration.
The description of enumerations is similar to structure and federation in the form of:
Enum enumeration name {
Identifier ① [= integer constant]
Identifier ② [= integer constant]
...
Identifier N [= integer constant]
} enumerate variables
2. Use
The use of enumerations is defined by the enum keyword, as follows:
Enum xxx {...}
The way to declare keywords as enumerated types is as follows:
/ / declare d to be an enumerated type Directionlet d: Direction
Types can be divided into:
Numeric enumeration
String enumeration
Heterogeneous enumeration
Numeric enumeration
When we declare that an enumerated type is, although no values are assigned to them, their values are actually the default numeric types, and the default accumulates from 0:
Enum Direction {Up, / / defaults to 0 Down, / / defaults to 1 Left, / / defaults to 2 Right / / defaults to 3} console.log (Direction.Up = 0); / / trueconsole.log (Direction.Down = 1); / / trueconsole.log (Direction.Left = 2); / / trueconsole.log (Direction.Right = 3); / / true
If we assign the first value, the subsequent values will also accumulate 1 based on the previous value:
Enum Direction {Up = 10, Down, Left, Right} console.log (Direction.Up, Direction.Down, Direction.Left, Direction.Right); / / 10 11 12 13 string enumeration
The value of an enumeration type can actually be a string type:
Enum Direction {Up = 'Up', Down =' Down', Left = 'Left', Right =' Right'} console.log (Direction ['Right'], Direction.Up); / / Right Up
If a variable is set as a string, the subsequent fields also need to be assigned a string, otherwise an error is reported:
Enum Direction {Up = 'UP', Down, / / error TS1061: Enum member must have initializer Left, / / error TS1061: Enum member must have initializer Right / / error TS1061: Enum member must have initializer} heterogeneous enumeration
Combine numeric enumeration with string enumeration, as follows:
Enum BooleanLikeHeterogeneousEnum {No = 0, Yes = "YES",}
In general, we rarely use heterogeneous enumerations.
Essence
Now an example of an enumeration is as follows:
Enum Direction {Up, Down, Left, Right}
After compilation, the javascript is as follows:
Var Direction; (function (Direction) {Direction [Direction ["Up"] = 0] = "Up"; Direction [Direction ["Down"] = 1] = "Down"; Direction [Direction ["Left"] = 2] = "Left"; Direction [Direction ["Right"] = 3] = "Right";}) (Direction | | (Direction = {}))
As you can see in the above code, Direction [Direction ["Up"] = 0] = "Up" can be divided into
Direction ["Up"] = 0
Direction [0] = "Up"
So after defining the enumeration type, you can get the corresponding value through positive and negative mapping, as follows:
Enum Direction {Up, Down, Left, Right} console.log (Direction.Up = 0); / / trueconsole.log (Direction [0]); / / Up
And multiple defined enumerations can be merged, as follows:
Enum Direction {Up = 'Up', Down =' Down', Left = 'Left', Right =' Right'} enum Direction {Center = 1}
After compilation, the js code is as follows:
Var Direction; (function (Direction) {Direction ["Up"] = "Up"; Direction ["Down"] = "Down"; Direction ["Left"] = "Left"; Direction ["Right"] = "Right";}) (Direction | (Direction = {})); (function (Direction) {Direction [Direction ["Center"] = 1] = "Center";}) (Direction | | (Direction = {}))
As you can see, the Direction object properties are overlaid
Third, application scenarios
Take back the example of life. The fields returned at the backend are marked with a date of 0-6, and enumerations can be used to improve the readability of the code, as shown below:
Enum Days {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; console.log (Days ["Sun"] = 0); / / trueconsole.log (Days ["Mon"] = 1); / / trueconsole.log (Days ["Tue"] = = 2); / / trueconsole.log (Days ["Sat"] = 6); / / true
Including when the backend returns 0, 1, and so on, we can define it through enumeration, which can improve the readability of the code and facilitate subsequent maintenance.
Thank you for reading! This is the end of this article on "sample analysis of enumerated types in TypeScript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.