JavaScript Switch Case Using Enumeration

I have an "enumeration" declared like this:

var PlaceType = { PASSABLE_TERRAIN: 1, IMPASSABLE_TERRAIN: 0, SOMEWHAT_PASSABLE_TERRAIN: 2, PATH: 3 }; 

and a function declared as follows:

 setPlaceType(placeType) { this.clear = false; this.placeType = placeType; alert("before switch "+(PlaceType.SOMEWHAT_PASSABLE_TERRAIN==this.placeType)); switch(this.placeType) { case PlaceType.PASSABLE_TERRAIN: { alert("Case PASSABLE"); break; } case PlaceType.IMPASSABLE_TERRAIN: { alert("Case IMPASSABLE"); break; } case PlaceType.SOMEWHAT_PASSABLE_TERRAIN: { alert("Case SOMEWHAT_PASSABLE"); break; } case PlaceType.PATH: { alert("Case PATH"); break; } default: { alert("case default"); } } } 

if I call it like this:

 setPlaceType(1); 

I get the following warnings: "before switch true", "case default"

if I call it like this:

 setPlaceType(2); 

I get the following warnings: "before switch false", "case default"

In other words, the function is called with the correct argument, which when executed (it seems to me) is the same comparison as the switch, but through "==" I get the correct behavior, but the switch never matches the values ​​in the corresponding case. Does anyone know why?

+4
source share
4 answers

The comparison operator will use both operands for strings if either of them is a string. If you pass a string, you compare string == number , which will pass the number to the string, and in the case of passing the string '2' it will be true.

case code comparison uses the identification operator === and crashes if the operands are not the same type.

a long story, make sure you always pass a number, if your cases are compared with numbers, you can double check this:

 setPlaceType(placeType) { if (typeof placeType !== 'number') { throw new Error('You must pass a number to setPlaceType!'); } ... } 

you should also call your function as follows:

 setPlaceType(PlaceType.PASSABLE_TERRAIN); 

Otherwise, it makes no sense to use the term "enumeration" (I use this term freely).

+5
source

The case of correspondence is determined using == the identification operator, and not the equality operator ==. Expressions must match without type conversion. This will not work if you pass a string and not an integer.

 setPlaceType(1); //"Case PASSABLE" setPlaceType("1"); //"case default" 

Code execution example with the above lines: jsFiddle

So, if you say that it fails, you are probably comparing a string to a number. Use parseInt.

 this.placeType = parseint(placeType,10); 
+1
source

Referring to this => Switch-Case for strings in Javascript does not work as expected

Switch to do === , and if to do == .

Hope this help! To have a good day

+1
source

When you perform a comparison using == , js uses type-coercion to cast the two operands to an intermediate type, the strings in this case and therefore to compare them.

So, to get the effect of working with your switch statement, you will need to use as such

 this.placeType = parseint(placeType); 

What you need to know here is that it is actually not an ideal practice to compare 2 values ​​in javascript using the == operator, use the === operator instead, which also checks for the same types. So in your case

 alert("before switch "+(PlaceType.SOMEWHAT_PASSABLE_TERRAIN==this.placeType)); 

would fail if you used === as you are comparing int and string

Working demo here: http://jsfiddle.net/pratik136/ATx8c/

0
source

Source: https://habr.com/ru/post/1438425/


All Articles