SWITCH javascript 总是执行默认情况的问题
好吧,我遇到了这个麻烦,我一直在处理,但我就是无法让它工作,
我的功能
function getDirections(dir)
{
var img;
switch(dir)
{
case 0:
img+='N.png';
break;
case 1:
img+='NE.png';
break;
case 2:
img+='E.png';
break;
case 3:
img+='SE.png';
break;
case 4:
img+='S.png';
break;
case 5:
img+='SO.png';
break;
case 6:
img+='O.png';
break;
case 7:
img+='NO.png';
break;
default:
alert('enetered default but direction='+dir);
}
return img;
}
很简单,对吧?现在我将此间隔设置为 5000 毫秒来调用 getDirections(variable),该函数在第一次调用时运行良好,但之后,它总是进入默认子句,并且还会提醒“已输入默认值,但方向=dirvalue”,我的意思是,即使 dir 是 0-7 之间的值,它也总是会输入默认值:但它会警告该值,因此应该输入其中一种情况。
我使用 else if 做了同样的事情,它有效,所以我不知道 SWITCH 有什么问题
if(dir==0){img+='N.png';}
else if(dir==1){img+='NE.png';}
else if(dir==2){img+='E.png';}
else if(dir==3){img+='SE.png';}
else if(dir==4){img+='S.png';}
else if(dir==5){img+='SO.png';}
else if(dir==6){img+='O.png';}
else if(dir==7){img+='NO.png';}
well i have this trouble and ive been dealing with but i just cant get it to work
i have this function
function getDirections(dir)
{
var img;
switch(dir)
{
case 0:
img+='N.png';
break;
case 1:
img+='NE.png';
break;
case 2:
img+='E.png';
break;
case 3:
img+='SE.png';
break;
case 4:
img+='S.png';
break;
case 5:
img+='SO.png';
break;
case 6:
img+='O.png';
break;
case 7:
img+='NO.png';
break;
default:
alert('enetered default but direction='+dir);
}
return img;
}
quite simple right? now i have this interval set to 5000 ms to call getDirections(variable), the function works well the first time its called but after that , it always enter in the default clause and it also alerts the 'entered default but direction=dirvalue' , i mean even if dir is a value between 0-7 it will always enter to default: but it would alert the value so it was supossed to enter to one of the cases.
i made the same using else if and it worked so i dont know what its wrong with SWITCH
if(dir==0){img+='N.png';}
else if(dir==1){img+='NE.png';}
else if(dir==2){img+='E.png';}
else if(dir==3){img+='SE.png';}
else if(dir==4){img+='S.png';}
else if(dir==5){img+='SO.png';}
else if(dir==6){img+='O.png';}
else if(dir==7){img+='NO.png';}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我知道我参加聚会有点晚了,但我认为这对于那些不明白为什么“如果”起作用而开关不起作用的人来说可能很重要。可能没有人会读这个答案,但我在搜索其他内容时发现了它,所以也许有人会发现这很有帮助:
你的开关是这样的:
这与一系列双等于(==)不同,而是一个三重等于系列 (===)。这相当于:
在“==”的世界中,整数2与字符串“2”相同,但在“===”的世界中则不同。
I know I'm a bit late to the party, but I thought it might be important for anyone who doesn't understand why the "ifs" worked and the switch didn't. It's likely no one will read this answer, but I found it while searching for something else, so perhaps someone will find this helpful anyway:
Your switch is this:
This is not the same as a series of double equals (==) but a series of triple equals (===). It would be equivalent to:
In the world of "==", the integer 2 IS the same as the string "2", but not in the land of "===".
我猜想由于某种原因 dir 被作为字符串传递。尝试将 case 1: 更改为 case '1':
I'd guess that for some reason dir is being passed in as a string. Try changing case 1: to case '1':
使用数组代替 if/else 块链或巨大的 switch 语句将更快、更灵活且不易出错。另外,您不必担心
dir
是数字还是字符串。而不是:您可以将文件名存储在数组中:
或者可以说更具可读性:
然后仅使用:
使用数组的 getDirections 完整实现将是:
它对您有用吗?
如果
images
仅在该函数中使用,那么您可能希望将其存储为函数的属性,以避免命名空间污染,如下所示:或使用闭包。
Using an array instead of a chain of if/else blocks or a giant switch statement will be faster, more flexible and less error-prone. Also, you wouldn't have to worry if
dir
is a number or a string. Instead of:you can store the file names in an array:
or arguably more readable:
and then use just:
Full implementation of getDirections using an array would be:
Does it work for you?
If
images
is used only in that one function then you may want to store it as a property of the function to avoid your namespace pollution like this:or use a closure.
很难解释为什么,但与所有其他情况一样,
default:
情况也需要在其后面有一个break;
语句。Hard to explain why, but the
default:
case also need abreak;
statement after it like all the other cases.我刚刚在 FireFox/FireBug 中运行代码并以这种方式调用该函数
第一个正确执行,接下来的两个输入
default
。它们是字符串而不是整数,这正是案例所寻找的。
我添加了
然后只有中间的进入了
default
。显然,您调用该函数的方式存在问题。它可能传递一个字符串。我还使用了您的
if-else
块(添加了else{alert(dir);}
并为每个调用返回了正确的值。Javascript可以进行即时转换(我认为有一个更好的词来形容)在字符串和整数(以及其他)之间,如果您更改
ifs< 中的比较,就会发生这种情况。 /code> 到
===
你会得到与switch
块相同的行为。I just ran the code in FireFox/FireBug and called the function this way
The first one does it correctly and the next two enter
default
.They are strings and not integer which is what the cases are looking for.
I added
and then only the middle one entered the
default
. Obviously there is an issue with the way you are calling the function. It is likely passing a string.I also used your
if-else
block (added anelse{alert(dir);}
and that returned the correct value for each call.Javascript can do on the fly conversion (I think there's a better word for that) between strings and ints (and others). This is occuring when you do the comparrison using
==
. If you change the comparison in theifs
to===
you get the same behavior as with theswitch
block.我将您的代码粘贴到 HTML 文件中,并使用以下按钮运行它:
当使用普通
2
调用 switch-version 时,它会按预期工作。使用'2'
调用它会使其跳转到default
行。 if-else 版本在这两种情况下都按预期工作。所以问题可能是switch
不执行隐式转换,而==
执行。I pasted your code into an HTML file and ran it with the following buttons:
When calling the switch-version with a plain
2
, it works as expected. Calling it with'2'
makes it drop through to thedefault
line. The if-else version works as expected in both cases. So the issue is probably thatswitch
doesn't do an implicit conversion and==
does.这很奇怪......尝试确保 dir 是一个 int,在切换之前执行此操作:
如果警报正确显示值,则它应该进入切换,但它仍然可以“看起来”正确,但具有不同的数据类型。手动进行转换以确保它是 int
That is weird... try to make sure that dir is an int, do this before the switch:
If the alert shows the value correctly it should enter the switch, but still it can "look" correct but be of a different data type. Do the conversion manually to ensure it's an int