This is the 20th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

The enumeration (Enum) type applies to scenarios in which the value is restricted within a certain range, for example, there are only seven days in a week from Monday to Sunday, three primary colors: red, green, and blue, and four directions: up, down, left, and right.

USES:

Using enumerations we can define numeric constants with names. Enumerations are defined by the enum keyword.

Digital enumeration

Example:

enum Direction{
    Up,
    Down,
    Left,
    Right,
}
console.log(Direction.Up);
Copy the code

Call our program with the TS-node command

Running results:

Here enumerators are assigned incrementing numbers starting from 0. In this case, UP is 0, Down is 1, and Right is 3

Enumeration values are also reversely mapped to enumeration names:

Example:

enum Direction{
    Up,
    Down,
    Left,
    Right,
}
console.log(Direction[0]);
Copy the code

Running results:

Principle of reverse mapping:

First let’s look at the compiled JavaScript code:

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 = {}));

console.log(Direction.Down);
console.log(Direction[0]);
Copy the code

First he declares a scope Direction,

And then Direction[“Up”] = 0 will assign Up in Direction to 0 because the JavaScript assignment operator returns the value that was assigned to it so Direction[“Up”] = 0 returns 0, So Direction[Direction[“Up”] = 0; Direction[0] = “Up”; This implements a two-way assignment process.

Manual assignment

We can also assign enumerations manually:

enum Direction{
    Up=10,
    Down,
    Left,
    Right,
}
console.log(Direction.Up);
Copy the code

In the above example, enumerators that are not manually assigned are incremented after the previous enumerator.

Running results:

String enumeration

Example:

enum Direction{
    Up = 'Up',
    Down = 'DOWN',
    Left = 'LEFT',
    Right = 'RIGHT',}Copy the code

We can do a simple string comparison:

enum Direction{
    Up = 'UP',
    Down = 'DOWN',
    Left = 'LEFT',
    Right = 'RIGHT',}const value = 'UP'
if(value === Direction.Up){
    console.log('go up! ');
    
}
Copy the code

Running results:

Constant enumeration (constant enumeration)

Constant enumerations are enumerations defined using const enum:

const enum Direction{
    Up = 'UP',
    Down = 'DOWN',
    Left = 'LEFT',
    Right = 'RIGHT',}const value = 'UP'
if(value === Direction.Up){
    console.log('go up! ');
    
}
Copy the code

Constant enumerations differ from regular enumerations in that they are removed at compile time and cannot contain computed members.

Compiled code:

var value = 'UP';
if (value === "UP" /* Up */) {
    console.log('go up! ');
}
Copy the code

Note: Only constant values can be used with constant enumerations. An error will be reported if calculated values are included

Example:

const enum Direction{
    Up ,
    Down,
    Left,
    Right = 'RIGHT'.length,
}
Copy the code

Error message:

expand

To use ts-Node in numeric enumeration, we need to install ts-Node because the TSC command in typescript doesn’t run typescript code directly. It is important to note that TS-Node does not equate to typescript node.js. It simply encapsulates typescript compilation and provides the ability to run typescript code directly.

Global installation commands:

npm install -g ts-node 
Copy the code