4

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.

4
  • That is what happens when a compiler writes JavaScript. Commented Jan 29, 2016 at 1:16
  • Actually this isn't what the compiler generates. See my answer stackoverflow.com/a/35075698/390330 Commented Jan 29, 2016 at 2:09
  • 2
    Arrays are Objects, so if all you are doing is assigning properties using square bracket notation, then the two behave exactly the same. Arrays have a special length property, but aren't special otherwise (their inherited properties are mostly generic and can be applied to any suitable object). Commented Jan 29, 2016 at 2:11
  • @RobG Thanks, this is what i did wonder. Commented Jan 29, 2016 at 4:24

4 Answers 4

5

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:

  1. by text to get its index (first four properties)
  2. by index to get its text (second four properties)
Sign up to request clarification or add additional context in comments.

3 Comments

but, console.log(Seson.length) // 4 Doesn't it mean Season is array? So, how can array have "Spring" property like object?
@monad98 the question you are asking is wrong. That is not the JavaScript that the compiler generates : stackoverflow.com/a/35075698/390330
@monad98 Array is an object too, with some specifics: Array.prototype.length is a last occupied integer index + 1.
4

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

1 Comment

Yes. But this is not what the op is questioning stackoverflow.com/a/35075698/390330
1

I'll address your question as to why it still works if you change the first statement in your code block to var Season = []; so that Season is an array instead of an empty object. Arrays are also objects, and you can set any property you like on an object in JavaScript, even arrays (as long as you don't run the object through Object.freeze). So the expression Season["Spring"] = 0 is simply setting a property named "Spring" on the Season object which you could access using Season["Spring"] or Season.Spring the same as you would on any other object.

Comments

0

if I change Season to {} empty objext at first line, this also works and it makes sense

I have some docs about enums, but your question is why:

Season = [];

Instead of

Season = {};

When in actuality the following TypeScript:

enum Season {
    Spring,
    Summer,
    Fall,
    Winter
}

Generates the JavaScript:

var Season;
(function (Season) {
    Season[Season["Spring"] = 0] = "Spring";
    Season[Season["Summer"] = 1] = "Summer";
    Season[Season["Fall"] = 2] = "Fall";
    Season[Season["Winter"] = 3] = "Winter";
})(Season || (Season = {}));

And as you can see Season = {}. So it should make sense to you.

PS:

Not your question but, if you are curious about Season[Season["Spring"] = 0] = "Spring"; that is covered here.

1 Comment

Thank you~! It helped me a lot for understanding enums

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.