Javascript ENUM pattern naming convention

31,636

Solution 1

According to Google's coding conventions this is the correct way indeed to name an enum in javascript.

As requested here is a link.

Solution 2

That is indeed the correct way to name the enum, but the enum values should be ALL_CAPS instead of UpperCamelCase, like this:

var WinnerEnum = {
    PLAYER_1: 1,
    PLAYER_2: 2,
    DRAW: 0
};

This is similar to Java's naming convention for enums.

Some references:

As with coding style in general, you'll find people doing things in many different ways, with each way having its own set of good reason. To make things easiest for anyone reading and working with your code, however, I would recommend using the style which has the most authoritative reference and therefore usually the most widespread adoption.

I couldn't find any reference more authoritative than Google's style guide and the writings above, written by people who have given some serious thought to enums, but I'd be interested to hear of any better references.

Share:
31,636
Benjamin Gruenbaum
Author by

Benjamin Gruenbaum

Hi, I'm Benjamin 👋 You can find me here or if you need to reach me. At my day job I work at Microsoft on JavaScript development infrastructure in WiCD. Before that I was in Testim.io working on automating test automation and empowering QA developers Before that I worked on distributed algorithms and platforms for the webrtc based Peer5 P2P CDN, before that I wrote ran the core dev team at TipRanks writing csharp and javascript. I also do a bit of open source, here are some projects I am involved with: Node.js node.js platform - core collaborator. Bluebird bluebird core team member and maintainer. MobX mobx core team. Sinon.JS sinon.js team member. If you want to get involved in Node.js (at any capacity, no experience required) please do reach out. I promise you don't need perfect English, l33t coding skills or to be a "bro" to fit in (but those are welcome too). My email is written in the node home page. If you have an interesting use case for async-iterators/generators in Node.js - we're interested in talking in particular :) I have gold tags in promise javascript and a few others because I've spent a year answering as many promises questions as I could back then. If you're building something cool with promises please don't hesitate to reach out. I hereby release any code, test or multimedia content written in any answer and/or question by me in StackOverflow and anywhere else in the Stack Exchange network as public domain. No acknowledgement is required (although it is appreciated).

Updated on February 07, 2020

Comments

  • Benjamin Gruenbaum
    Benjamin Gruenbaum about 4 years

    I am working on a javascript project which requires use of javascript "Enums" meaning Objects like:

    var WinnerEnum = {
                Player1: 1,
                Player2: 2,
                Draw: 0
        };
    

    This is working great for me, however, I have no idea what is the proper way (according to convention) to name the Enum because as far as I know only class names start with a capital letter (indicating the ability to call a constructor on).

    JSHint also outputs the following warning:

    Missing 'new' prefix when invoking a constructor.
    

    If there is no convention, I would appreciate a good way to name enums that would not confuse them with class names. Update 2014 : JSHint no longer does this.