Javascript ENUM Template Naming Convention

I am working on a javascript project that requires the use of javascript "Enums", meaning objects like:

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

This works fine for me, however, I don’t know how to correctly (according to the convention) name Enum, because as far as I know, only class names begin with a capital letter (indicating the possibility of calling the constructor).

JSHint also displays the following warning:

 Missing 'new' prefix when invoking a constructor. 

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

+6
source share
2 answers

According to Google's coding conventions, this is the right way to really name an enum in javascript.

As requested here is the link .

+2
source

This is indeed the correct way to name an enumeration, but the enumeration values ​​must be ALL_CAPS instead of UpperCamelCase, for example:

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

This is similar to the Java naming convention for enumerations.

Some links:

As with the coding style in general, you will find that people do things in different ways, each of which has its own set of reasonable reasons. However, to make things easier to read and work with your code, I would recommend using a style that has the most authoritative link and, as a rule, the most common use.

I could not find the links more authoritative than the Google style guide and the above works written by people who seriously thought about the listings, but I would be interested to know about any better links.

+1
source

All Articles