How would we or should we carry over Java or other C-like casing conventions for constants to JavaScript?
In Java we would name a "constant" (static final) member with ALL_UPPERCASE. If we had a set of values we wanted as constants, we could use an enum member, and we'd name the enum member with a HumpBack name, but the seperate enum's would be UPPERCASE, e.g. SomeClass.SomeEnum.SOME_VALUE
.
So lets say I have a JavaScript library used for some Star Trek game (picking a silly example), with a root namespace object StarTrek
. I've got some constant values (never mind that Js has no constants) I want to build in for alien species, attributes of planets. Without thinking about the Java conventions, I'd have went for something like:
StarTrek.SPECIES = {
HUMAN: "HUMAN",
KLINGON: "KLINGON",
VULCAN: "VULCAN"
};
StarTrek.PLANET_ATTRIBUTE = {
CLASS: {
M: "M",
T: "T",
...
},
LIFE_FORMS: {
NONE: "NONE",
INTELLIGENT: "INTELLIGENT",
NO_INTELLIGENT: "NO_INTELLIGENT"
}
};
Note that unlike Java, we have something that is like a nested "enum" here in the PLANET_ATTRIBUTES
. I've went for uppercase "enum" names here because it feels like everything below StarTrek.
is essentially supposed to be "constant" (vs in Java where the enum is actually a Class, hence the BumpyCase name). So anyway I could specify that there where no intelligent lifeforms by using StarTrek.PLANET_ATTRIBUTE.LIFE_FORMS.NO_INTELLIGENT
.
Now, following Java's conventions I would have gone with something like:
StarTrek.Species = {
HUMAN: "HUMAN",
KLINGON: "KLINGON",
VULCAN: "VULCAN"
};
StarTrek.PlanetAttribute = {
Class: {
M: "M",
T: "T",
...
},
LifeForms: {
NONE: "NONE",
INTELLIGENT: "INTELLIGENT",
UNINTELLIGENT: "UNINTELLIGENT"
}
};
In which case I would specify that there where no intelligent lifeforms by using StarTrek.PlanetAttribute.LifeForms.NO_INTELLIGENT
.
Which fits JavaScript better and why? Are there any conventions proposed for this?