JavaScript switch case using enum
The comparison operator will cast both operands to strings if either operator is a string. If you pass in a string, you are comparing string == number
which will cast the number to a string and, in the case of passing the string '2'
, it will be true.
switch case comparison uses the identity operator ===
and will fail if the operands are not the same type.
long story short, make sure you are always passing a number if your cases are comparing against numbers, you can double check like this:
setPlaceType(placeType) {
if (typeof placeType !== 'number') {
throw new Error('You must pass a number to setPlaceType!');
}
...
}
also, you should be calling your function like this:
setPlaceType(PlaceType.PASSABLE_TERRAIN);
otherwise there's not really any point to using the "enumeration" (i use that term loosely) object.