When working with typescript, the following code is used to define an enum:
enum days { sun = 1, mon = 0, tues };
The typescript code above will be compiled into the following javascript equivalent:
var days;
(function (days) {
days[days["sun"] = 1] = "sun";
days[days["mon"] = 0] = "mon";
days[days["tues"] = 1] = "tues";
})(days || (days = {}));
;
The key line in this process is days[days["sun"] = 1] = "sun";
Breaking it down further:
- This part ensures that calling
days.sun
will return a value of 1
- It also sets the value at key 1 as "sun", meaning initially
days[1]
is set to "sun"
Similarly, days[days["mon"] = 0] = "mon";
- Ensures that calling
days.mon
will give a value of 0
- Sets the value at key 0 as "mon", thus
days[0]
will be "mon"
However, the third part days[days["tues"] = 1] = "tues";
Involves setting days["tues"] = 1
which:
- Allows for calling
days.tues
and getting 1 as the value
- Returns the value set at key "tues" as 1
With this operation, days[1]
will be replaced with the value "tues"