Over a million developers have joined DZone.

Enums for JavaScript

· Web Dev Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

This post describes a JavaScript implementation of enums, enumerations of symbols. The idea originally comes from Allen Wirfs-Brock, via a thread on the es-discuss mailing list.

Using an enum

    var color = new enum.Enum("red", "green", "blue");
    function isGreen(c) {
        return c === color.green;
    }
color.green is an object (an instance of Symbol) and a === b is only true if a and b are the same object, so the above works out nicely. Interaction:
    > isGreen(color.red)
    false
    > isGreen(color.green)
    true
The cool thing is that switch works, too:
    function translate(c) {
        switch(c) {
            case color.red:
                return "rot";
            case color.green:
                return "grün";
            case color.blue:
                return "blau";
        }
    }
Interaction:
    > translate(color.red)
    'rot'
You can also give enum symbols custom properties:
    var color = new enum.Enum({
        red: { de: "rot" },
        green: { de: "grün" },
        blue: { de: "blau" },
    });
Then translate() becomes simpler:
    function translate(c) {
        return c.de;
    }
Lastly, enum symbols can also be converted to string:
    > console.log("The sky is "+color.blue);
    The sky is |blue|
    > console.log("The sky is "+color.blue.name);
    The sky is blue


The implementation

You can download the enum project on GitHub. It consists of two constructors:

  • Symbol implements the symbols that an enum holds. Symbols are immutable.
  • Enum implements enums. Each enum is an object that maps symbol names to symbol instances. Enums are immutable.

There are several measures to make instances of Symbol as immutable as possible:

  • The prototype of symbols is frozen (immutable).
  • The prototype of symbols has no prototype. Normally, Symbol.prototype would have the prototype Object.prototype (which comes with several standard methods etc.). But Object.prototype is mutable which we want to avoid.
  • Instances of Symbol are frozen.

Note: Without Object.prototype in the prototype chain, we need to provide a toString() method. Otherwise symbols cannot be converted to string, e.g. to display them on a command line.

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Axel Rauschmayer, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}