Reading the 'enum' in typescript, I see this code compiled to javascript.
var Season = [];
Season[Season["Spring"] = 0] = "Spring";
Season[Season["Summer"] = 1] = "Summer";
Season[Season["Fall"] = 2] = "Fall";
Season[Season["Winter"] = 3] = "Winter";
console.log(Season.Spring); // 0
console.log(Season[0]); // Spring
and, if I change Season to {} empty objext at first line, this also works and it makes sense. I don't know what's happening hear. What is this?
Edit: Yes. This is not what compiler generate. Compiler uses empty object. But if I changed it to empty array. It still works. My question was why array also works good. At first my question included both version, but someone edited question and delete object-use version.
This:
Season[Season["Spring"] = 0] = "Spring";
Season[Season["Summer"] = 1] = "Summer";
Season[Season["Fall"] = 2] = "Fall";
Season[Season["Winter"] = 3] = "Winter";`
Creates eight properties in Season object:
Season["Spring"] = 0;
Season["Summer"] = 1;
Season["Fall"] = 2;
Season["Winter"] = 3;
Season[0] = "Spring";
Season[1] = "Summer";
Season[2] = "Fall";
Season[3] = "Winter";
After that Season can be requested:
Looks like someone is just trying to make Season be a bidirectional map.
In other words, you can look up items by numeric index or by their string value.
var season1 = Season[1]; // season1 == 'Summer'
var summerIndex = Season['Summer']; // summerIndex == 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With