DEV Community

loading...

Discussion on: MVC Calculator in "almost" Vanilla JS

Collapse
aminnairi profile image
Amin

Hi there.

You said in your first example that this was how to make an enumeration in JavaScript:

const Color = {
   RED = 0,
   GREEN = 1,
   BLUE = 2
};

I believe the right example was:

const COLOR = {
  RED: 1,
  GREEN: 2,
  BLUE: 3
};

The equal sign is not a valid operator for assigning value to a property inside of an object in JavaScript.

Collapse
corvusetiam profile image
CorvusEtiam Author

Yes, you are right. That is what you get, after using TS too much :).

I would even go as far to tell the best way would be something like this:

const COLOR = Object.freeze({
  RED: 1,
  GREEN: 2,
  BLUE: 3
});

Thanks.

Collapse
aminnairi profile image
Amin

Haha, I knew that! Yes I though about adding the Object.freeze, but I didn't know whether you wanted to go this far or not. But it's great that you added a fully capable example.

Great article. Keep up the good work!