[英]How to use !-1 in javascript switch-case function
I "want" to use switch, but i can't seem to get it to work when I use it in this fashion.我“想要”使用 switch,但是当我以这种方式使用它时,我似乎无法让它工作。 Anyone know why?
有谁知道为什么?
var mystring='my name is johnny';
switch (!-1) {
case mystring.indexOf('hello'):
alert('hello');
break;
case mystring.indexOf('goodbye'):
alert('goodbye');
break;
case mystring.indexOf('johnny'):
alert('johnny');
break;
default:
alert('default');
}
it always alerts "default", but you can see that I want it to alert "johnny"它总是提醒“默认”,但你可以看到我希望它提醒“约翰尼”
Disclaimer: This switch is evil .免责声明:这个开关是邪恶的。 Use if/else-if statements.
使用 if/else-if 语句。 but if you must use a switch it can be done as such:
但如果您必须使用开关,则可以这样做:
switch (true) {
case /hello/.test(mystring):
alert('hello');
break;
case /goodbye/.test(mystring):
alert('goodbye');
break;
case /johnny/.test(mystring):
alert('johnny');
break;
default:
alert('default');
}
Should work as you want it to.应该按您的意愿工作。
I would be tempted to refactor it further.我很想进一步重构它。
function Parse(str) {
var logic = {
"hello": function(str) { alert("hello"); }
/*, ... */
}
for (var k in logic) {
if ((new RegExp(k)).test(str)) {
logic[k](str);
}
}
}
Parse("Hello I am johnny");
!-1
coerces -1
to a boolean, and then negates it, so !-1
将-1
强制为 boolean,然后将其取反,所以
switch(!-1)
{
// stuff
}
is equivalent to相当于
switch(false)
{
// stuff
}
Since there is no case false
, the default
will always be executed.由于没有
case false
,因此将始终执行default
。 This is really not the right way to use a switch — there is no point in switch
ing on a constant expression.这确实不是使用 switch 的正确方法——在常量表达式上
switch
是没有意义的。
Why do you "want" to use a switch
?为什么你“想要”使用
switch
?
Here's how to implement it correctly:以下是如何正确实现它:
var mystring='my name is johnny';
if (mystring.indexOf('hello') !== -1) alert('hello');
else if (mystring.indexOf('goodbye') !== -1) alert('goodbye');
else if (mystring.indexOf('johnny') !== -1) alert('johnny');
Or, less repetetively,或者,不那么重复,
var mystring='my name is johnny',
words = ['hello', 'goodbye', 'johnny'],
word;
while (word = words.shift(), mystring.indexOf(word) === -1);
word = word || 'default';
alert(word);
Your combination of "not"( !
) and " -1
" seems faulty.您的 "not"(
!
) 和 " -1
" 的组合似乎有问题。 You need to move them to your cases like this您需要像这样将它们移动到您的案例中
var mystring='my name is johnny';
switch (true) {
case mystring.indexOf('hello') != -1:
alert('hello');
break;
case mystring.indexOf('goodbye') != -1:
alert('goodbye');
break;
case mystring.indexOf('johnny') != -1:
alert('johnny');
break;
default:
alert('default');
}
It's a pain in PHP that strpos(...)
returns false
when not found and 0
when string starts with it (and those 2 values are ==
, but not ===
) but I personally find JavaScripts indexOf(..) == -1
even worse.在 PHP 中,
strpos(...)
在未找到时返回false
,当字符串以它开头时返回0
(这两个值是==
,但不是===
),但我个人发现 JavaScripts indexOf(..) == -1
更糟。 (Why can't it be false
or null
?) (为什么不能是
false
的或null
?)
What's happening here is you're switch
ing the result of !-1
and that case
isn't found.这里发生的事情是您正在
switch
!-1
的结果,但找不到这种case
。 switch-case
doesn't seem to be the right construct to use here. switch-case
似乎不是在这里使用的正确结构。
The expression !-1
will just evaluate to false
.表达式
!-1
只会计算为false
。
You are trying to convert a comparison like x != -1
into x == !-1
, but the !
您正在尝试将
x != -1
之类的比较转换为x == !-1
,但是!
operator doesn't negate the comparison, only the value.运算符不会否定比较,只有值。
I don't think your implementation is correct.我不认为你的实现是正确的。 The proper conditional statement to use is if... else .
要使用的正确条件语句是if... else 。
var mystring='my name is johnny';
var mystring='我叫约翰尼';
if(mystring.indexOf('hello');= -1) alert('hello');
if(mystring.indexOf('hello');= -1) alert('hello');
else if(mystring.indexOf('goodbye');= -1) alert('goodbye');否则 if(mystring.indexOf('goodbye');= -1) alert('goodbye');
else if(mystring.indexOf('johnny');= -1) alert('johnny');否则 if(mystring.indexOf('johnny');= -1) alert('johnny');
else alert('default');否则警报('默认');
The switch should be mystring, example:开关应该是mystring,例如:
http://www.w3schools.com/js/js_switch.asp http://www.w3schools.com/js/js_switch.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.