1. OverviewAn enumeration type is a name given to a set of values. For example, if we need to define a set of roles and need to use numbers to represent them, we can use the following code to locate them: enum role{ STUDENT, TEACHER, ADMIN } In the above code, we define role as an enumeration type, which contains three values. TypeScript will automatically assign serial numbers to each value, starting from 0 by default, and their values are 0 1 2. Of course, we can also customize each value. If not all are defined, the subsequent values will be incremented according to the previous values. The sample code is as follows: enum role1 { student = 1, // The next two values are 2 and 3 respectively teacher, admin, } enum role2 { // Each name has a specified value student = 1, teacher = 3, admin = 6, } 2. Digital EnumerationThe example we introduced above is a numeric enumeration type, but there is another point to note that if a field uses a constant or calculated value, we must set the initial value immediately following the field, otherwise an exception will be thrown. The sample code is as follows: ;(function () { // Define a function const getValue: () => number = (): number => { return 0 } enum role1 { student = getValue(), // teacher, // error Enumeration members must have an initializer expression. // admin, // error Enumeration members must have an initializer expression. } const TEACHER_ROLE: number = 3 // Each name has a specified value enum role2 { student, teacher = TEACHER_ROLE, // admin, // error Enumeration members must have an initializer expression. } })() 2.1 Reverse Mapping The so-called reverse mapping is that you can access We can get each specific value through .name or ['name'], or we can get the name corresponding to each value through [value]. The sample code is as follows: enum role { student, teacher, admin, } console.log(role.admin) // 2 console.log(role['teacher']) //1 console.log(role[0]) //'student' In fact, the enumeration type in The compiled code is as follows: "use strict"; var role; (function (role) { role[role["student"] = 0] = "student"; role[role["teacher"] = 1] = "teacher"; role[role["admin"] = 2] = "admin"; })(role || (role = {})); This may be easier to understand. In fact, it is to assign values to the role object through a self-adjusting function. After the assignment, it is as follows: var role = { "student": 0, "teacher": 1, "admin" : 2, 0: "student", 1: "teacher", 2: "admin", } It is worth noting that reverse mapping is only supported in numeric enumerations, not in string enumerations added in version 2.4. 3. String EnumerationThe so-called string enumeration means that the value of each field in the enumeration must be a string, or other fields in the enumeration. The sample code is as follows: enum Person { name = 'A bowl of Zhou', hobby = 'coding', // Set the field in the enumeration as the value myName = name, } console.log(Person.name, Person.myName) // One bowl for one week 4. const enumeration After we define a normal enumeration, a corresponding object will be created after it is compiled into The The sample code is as follows: const enum role { student, teacher, admin, } let admin = role.admin The above code will be compiled as follows: let admin = 2 /* admin */; 5. Summary This article introduces two basic enumeration types: numeric enumeration and string enumeration. Array enumeration also has a concept of reflection mapping, which means that you can access the value through the key and the key through This is the end of this article about You may also be interested in:
|
<<: Implementation of MySQL scheduled database backup (full database backup)
>>: Let IE6, IE7, IE8 support CSS3 rounded corners and shadow styles
1. Understanding of transition attributes 1. The ...
In this experiment, we configure MySQL standard a...
Fault description percona5.6, mysqldump full back...
On a Windows server, if you want to back up datab...
Preface As we all know, the browser's homolog...
This article example shares the specific code of ...
Preface As you know, Linux supports many file sys...
Table of contents introduce Support Intel CPU Sup...
Overlay network analysis Built-in cross-host netw...
<br />I just saw the newly revamped ChinaUI....
For security reasons, MySql-Server only allows th...
How to write transparent CSS for images using filt...
Table of contents Linux environment variables and...
How to install iso files under Linux system? Inst...
Original derivative command: bin/sqoop import -co...