Digital enumeration
enum Role {
Reporter = 1,
Developer,
Maintainer,
Owner,
Guest
}
console.log(Role.Reporter) / / 1
console.log(Role.Developer) / / 2
console.log(Role[2]) // 'Developer'
Copy the code
The default value of the first member is 0 and subsequent members are incremented, but it is possible to assign an initial value to each member, either to index a number by a string or to index a string by a number
String enumeration
enum Message {
Success = 'success',
Fail = 'fail'
}
console.log(Message.Success) // 'success'
console.log(Message['success']) // undefined
Copy the code
Success can only be indexed by Success, not the other way around
Heterogeneous enumeration
enum Answer {
N,
Y = 'Yes'
}
console.log(Answer.N) / / 0
console.log(Answer.Y) // 'Yes'
Copy the code
It is not recommended to use an enumeration that contains both numbers and strings
Members of the enumeration
enum Char {
// const member (const enumerator)
a,
b = Char.a,
c = 1 + 3.// Computed Member (Counting enumerators)
d = Math.random(),
e = '123'.length,
f = 4
}
Copy the code
Constant enumerators are evaluated at compile time and appear in the runtime as constant values. Computed enumerators are not evaluated at compile time but are reserved until run time, when the results are evaluated. Note: The enumerator after the evaluated member must be assigned an initial value
// ts compiled result
"use strict";
var Char;
(function (Char) {
// const member (const enumerator)
Char[Char["a"] = 0] = "a";
Char[Char["b"] = 0] = "b";
Char[Char["c"] = 4] = "c";
// Computed Member (Counting enumerators)
Char[Char["d"] = Math.random()] = "d";
Char[Char["e"] = '123'.length] = "e";
Char[Char["f"] = 4] = "f";
})(Char || (Char = {}));
Copy the code
Constant enumeration
Enumerations defined with const are called constant enumerations and are typically removed after compilation
const enum Month {
Jan,
Feb,
Mar,
Apr = Month.Mar + 1,
}
let month = [Month.Jan, Month.Feb, Month.Mar]
Copy the code
Constant enumerations can be used when we do not need an object, but need the value inside the object. Constant enumerations are removed after compilation, reducing the compiled code and making the code look cleaner.
// Compiled code
"use strict";
let month = [0 /* Jan */.1 /* Feb */.2 /* Mar */];
Copy the code
Enumerated type
There are three types of enumeration:
- Enumerator has no initial value
- Enumerators are numeric enumerators
- Enumerators are string enumerations
In some cases, both enumerations and enumerators can exist as separate types
Enum F {a = 0, b = 1} // Enumerators are string enumerators G {a = 'apple', Let E: E = 3 let F: F = 3 E === F let E: E = 3 let F: F === F E.a = 3 let e2: E.b = 3 let e3: E.a = 3 console.log(e1 === e2) // E1 and e2 are different enumerator types and an error is reported console.log(e1 === e3) // true E1 and e3 are the same enumerator types, Can be compared. // The enumeration value of a string can only be the value of an enumeration member. Let g1: G = g. let g2: G.a = G.aCopy the code