February 2019
Beginner
694 pages
18h 4m
English
When TypeScript generates the JavaScript for enums, it uses the closure and Immediately Invoked Function (IIF) patterns to define a JavaScript object with the correct properties that can be used as an enum. Let's take a look at this implementation. Given our DoorState enum, which is defined as follows:
enum DoorState {
Open,
Closed,
Ajar
}
We know that we can reference the DoorState.Open value, which will be represented as a numeric value of 0, Closed as 1, and Ajar as 2. But what happens if we reference an enum using an array-type syntax, as follows:
var ajarDoor = DoorState[2];
console.log(`ajarDoor is : ${ajarDoor}`);
Here, we assign the variable ajarDoor to an enum value based on the second index value of the DoorState ...
Read now
Unlock full access