Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>UPDATE</strong>: Thanks for all the upvotes everyone, but I don't think my answer below is the best way to write enums in Javascript anymore. See my blog post for more details: <a href="https://stijndewitt.com/2014/01/26/enums-in-javascript/" rel="noreferrer">Enums in Javascript</a>.</p> <hr> <p>Alerting the name is already possible:</p> <pre><code>if (currentColor == my.namespace.ColorEnum.RED) { // alert name of currentColor (RED: 0) var col = my.namespace.ColorEnum; for (var name in col) { if (col[name] == col.RED) alert(name); } } </code></pre> <p>Alternatively, you could make the values objects, so you can have the cake and eat it too:</p> <pre><code>var SIZE = { SMALL : {value: 0, name: "Small", code: "S"}, MEDIUM: {value: 1, name: "Medium", code: "M"}, LARGE : {value: 2, name: "Large", code: "L"} }; var currentSize = SIZE.MEDIUM; if (currentSize == SIZE.MEDIUM) { // this alerts: "1: Medium" alert(currentSize.value + ": " + currentSize.name); } </code></pre> <p>In Javascript, as it is a dynamic language, it is even possible to add enum values to the set later:</p> <pre><code>// Add EXTRALARGE size SIZE.EXTRALARGE = {value: 3, name: "Extra Large", code: "XL"}; </code></pre> <p>Remember, the fields of the enum (value, name and code in this example) are not needed for the identity check and are only there for convenience. Also the name of the size property itself does not need to be hardcoded, but can also be set dynamically. So supposing you only know the name for your new enum value, you can still add it without problems:</p> <pre><code>// Add 'Extra Large' size, only knowing it's name var name = "Extra Large"; SIZE[name] = {value: -1, name: name, code: "?"}; </code></pre> <p>Of course this means that some assumptions can no longer be made (that value represents the correct order for the size for example). </p> <p>Remember, in Javascript an object is just like a map or hashtable. A set of name-value pairs. You can loop through them or otherwise manipulate them without knowing much about them in advance.</p> <p>E.G:</p> <pre><code>for (var sz in SIZE) { // sz will be the names of the objects in SIZE, so // 'SMALL', 'MEDIUM', 'LARGE', 'EXTRALARGE' var size = SIZE[sz]; // Get the object mapped to the name in sz for (var prop in size) { // Get all the properties of the size object, iterates over // 'value', 'name' and 'code'. You can inspect everything this way. } } </code></pre> <p>And btw, if you are interested in namespaces, you may want to have a look at my solution for simple but powerful namespace and dependency management for javascript: <a href="http://packagesinjavascript.wordpress.com/" rel="noreferrer">Packages JS</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload