0

I wanted to create enum in javascript and I did the following code and I want to verify that this is the right way.

    var dataStates = {
    withMock: "withM",
    withoutMock: "withoutM",
};

is it ok?

I want for example to use it like

oCation.dataMode === dataStates.withMock

and that state.data.withMock will hold the value but Im not sure if it's the right way...

1
  • There are any number of ways to do stuff like this in JS--it really depends on what you mean by an "enum". This is just an object; it it works then it's "right", with the caveat that there's nothing to prevent errant code re-assigning a value. Commented Feb 2, 2015 at 16:11

2 Answers 2

1

What you have created is an object with named properties. This is not an Enum in the Java sense. Which is to say there are no language constraints enforcing uniqueness and single instances. However what you have done will meet the requirements you specified in your question.

Sign up to request clarification or add additional context in comments.

Comments

1

Javascript does not have a proper (static) enum type, but you can achieve something very close using the same code Typescript generates for its enums.

To offer enums but still be valid JS, you want an object where each enum element (name) is a key, mapping to a unique value (typically an int, since most languages with enums back them with integer values).

For an enum like enum Color {Red, Green, Blue};, you would end up with code containing:

Color[Color["Red"] = 0] = "Red";
Color[Color["Green"] = 1] = "Green";
Color[Color["Blue"] = 2] = "Blue";

This creates an object with a key for each enum element's name, and for each element's value. The final object looks like:

var Color;

(function(Color) {
  Color[Color["Red"] = 0] = "Red";
  Color[Color["Green"] = 1] = "Green";
  Color[Color["Blue"] = 2] = "Blue";
})(Color || (Color = {}));

document.getElementById("result").textContent = JSON.stringify(Color);
<pre id="result"></pre>

This allows you to access the enum values using the traditional Color.Red syntax, but also allows you to map values back into names using Color[Color.Red] (or a variable containing one of the enum values).

For example:

var Color;

(function(Color) {
  Color[Color["Red"] = 0] = "Red";
  Color[Color["Green"] = 1] = "Green";
  Color[Color["Blue"] = 2] = "Blue";
})(Color || (Color = {}));

document.getElementById("red").textContent = Color.Red; // Should print "0"

var currentColor = Color.Green;
document.getElementById("green").textContent = Color[currentColor]; // Should print "Green"

currentColor = Color.Blue;
document.getElementById("blue").textContent = Color[currentColor]; // Should now print "Blue"
<pre id="red"></pre>
<pre id="green"></pre>
<pre id="blue"></pre>

7 Comments

What does that give you over an object like this: var Color = { Red : 0, Green : 1, Blue : 2 };
@bhspencer That only allows you to use Color.Red === 0 and get the value, not Color[Color.Red] === "Red" and get the name from the value.
I see, It lets you get the string name if all you know is the int index. Thank you.
Consider instead of using ints for the values you use Strings. e.g. var Color = { Red : "Red", Green : "Green", Blue : "Blue" }; Now you can do comparison and have the name in something that seems a fair bit simpler.
@bhspencer No, that breaks relatively fundamental expectations around how enums should behave. Having an enum be names representing (typically consecutive) integer values is rather widespread.
|

Your Answer

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