javascript evt = (evt) 的含义?evt : window.event
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12635142/
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
Meaning of evt = (evt) ? evt : window.event
提问by amesh
Hi what does this javascript snippet means.(evt) part is so confusing.. evt is not a boolean. How it works?
嗨,这个 javascript 代码段是什么意思。(evt) 部分是如此令人困惑.. evt 不是布尔值。怎么运行的?
function checkIt(evt) {
evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
}
回答by Blender
evt = (evt) ? evt : window.event
is just the inline if
syntax. It's equivalent to this code:
evt = (evt) ? evt : window.event
只是内联if
语法。它等效于以下代码:
if (evt) {
evt = evt;
} else {
evt = window.event;
}
If evt
is truthy, evt
will be left alone. If evt
isn't truthy, it will be replaced with window.event
.
如果evt
是真实的,evt
将被单独留下。如果evt
不是真的,它将被替换为window.event
.
回答by Norguard
It's for event listeners.
它用于事件侦听器。
IE6-IE8 used a totally different event method than the W3C standard.
IE6-IE8 使用了与 W3C 标准完全不同的事件方法。
When an event fires, a W3C-standard browser will pass an event object in the callback:
当事件触发时,W3C 标准浏览器将在回调中传递一个事件对象:
function keyPressed (e) { /* do stuff with e */ }
In your case, it's keydown
(or something else using keyCode
).
IE didn't support this, instead it had window.event
which was updated every time an event happened.
在您的情况下,它是keydown
(或其他使用keyCode
)。
IE 不支持这一点,而是在window.event
每次事件发生时更新。
So your function is checking to see if an object was passed into it:
所以你的函数正在检查一个对象是否被传递给它:
evt = (evt) ? evt : window.event;
// does `evt` exist, and is it anything but '', 0, false, null, undefined, NaN
// yes: evt = itself (W3C)
// no: evt = window.event (IE6-8)
Then the code asks if evt.which
exists, to try to figure out where to get the keyCode from.
evt.keyCode
is what you should be using for modern browsers, in the case of keydown
and keyup
.
然后代码询问是否evt.which
存在,以尝试找出从何处获取 keyCode。
evt.keyCode
是你应该使用现代浏览器的东西,在的情况下keydown
和keyup
。
回答by jAndy
Assignment expressions like that are evaluated from right to left, so this means:
像这样的赋值表达式是从右到左计算的,所以这意味着:
- if
evt
has a truthyvalue, assign this value back toevt
- if not, assign the value of
window.event
regardless of its content toevt
- 如果
evt
有一个真值,则将此值分配回evt
- 如果不是,则
window.event
不管其内容如何分配值evt
回答by codebox
It means: if the evt
parameter has a value then keep the value, if it doesn't have a value then use window.event
instead.
这意味着:如果evt
参数有值则保留该值,如果它没有值则window.event
改为使用。
The ?
and ':' symbols are part of the ternary if operator:
的?
和“:”符号是三元如果操作者的一部分:
var w = x ? y : z;
so above you assign either y
or z
to w
depending on whether x
is considered to be a true or false value.
所以你上面指定要么y
还是z
以w
取决于是否x
被认为是一个true或false值。
If the checkIt
function was called without passing in an evt
argument i.e. checkIt()
then inside the function the evt
variable will have the value of undefined
which is treated as false
within an if condition.
如果checkIt
函数在没有传入evt
参数的情况下被调用,即checkIt()
在函数内部,evt
变量的值undefined
将被视为false
在 if 条件内。