javascript var charCode = (evt.which) ? evt.which : event.keyCode;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9318255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript var charCode = (evt.which) ? evt.which : event.keyCode;
提问by Painkiller
how a code var charCode = (evt.which) ? evt.which : event.keyCode
could be explaned?
what happens here ?
all i understand is that clause returns buttons value to the object charCode.
but what those ? and : signs mean?
and can i use this thing in other languager? java/c++/php and so on?
Thanks
如何解释代码var charCode = (evt.which) ? evt.which : event.keyCode
?这里会发生什么?我所理解的是该子句将按钮值返回给对象 charCode。但那些是什么?和:标志是什么意思?我可以在其他语言中使用这个东西吗?java/c++/php 等等?谢谢
回答by James Allardice
It's called the ternary conditional operator. It's basically short for an if...else
:
它被称为三元条件运算符。它基本上是 an 的缩写if...else
:
var charCode;
if(evt.which) {
charCode = evt.which;
}
else {
charCode = evt.keyCode;
}
Basically, it evaluates the first operand. If that evaluation returns true
, the second operand is returned. If false
, the third is returned.
基本上,它评估第一个操作数。如果该评估返回true
,则返回第二个操作数。如果false
,则返回第三个。
As for whether you can use it in other languages, you often can. From the languages you listed, Java and PHP both have it, and I'd be very surprised if C++ didn't (edit - a quick Google reveals that C and C++ do indeed support it too). For more, see Wikipedia.
至于是否可以在其他语言中使用它,您经常可以。从你列出的语言来看,Java 和 PHP 都有,如果 C++ 没有,我会非常惊讶(编辑 - 一个快速的谷歌显示 C 和 C++ 确实也支持它)。有关更多信息,请参阅维基百科。
回答by Konrad Borowski
First, of all, var charCode =
starts assignment to local charCode
variable. Next, ternary operatoris used. It compounds of three parts, condition, what happens if it's true and what happens if it's false.
首先,var charCode =
开始对局部charCode
变量赋值。接下来,使用三元运算符。它由三部分组成,条件,如果是真的会发生什么,如果是假的会发生什么。
(evt.which) ? evt.which : event.keyCode
# condition # if true # if false
In this case, it's used for feature detection (keyboard key event). evt.which
is proper way to do it, but in very old browsers you may want to use event.keyCode
.
在这种情况下,它用于特征检测(键盘按键事件)。evt.which
是正确的方法,但在非常旧的浏览器中,您可能想要使用event.keyCode
.
回答by rob
Others correctly pointed out that it is shorthand for:
其他人正确地指出它是以下的简写:
var charCode;
if(evt.which) {
charCode = evt.which;
}
else {
charCode = evt.keyCode;
}
but it is also longhand for:
但它也适用于:
var charCode = evt.which || evt.keyCode;
回答by Ash Burlaczenko
This is called the conditional operator.
这称为条件运算符。
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Conditional_Operator
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Conditional_Operator
To the left of the ?
is the condition. To the right are to results seperated by a :
. If the condition is true, the result on the left of the colon is used, otherwise the it's the result on the right.
的左边?
是条件。右边是由 a 分隔的结果:
。如果条件为真,则使用冒号左侧的结果,否则使用冒号右侧的结果。