javascript 这是什么意思..."var evt=event||window.event;"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3493033/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:20:01  来源:igfitidea点击:

What is the meaning of this..."var evt=event||window.event;"

javascript

提问by Suraj Air

What does the following mean in JavaScript?

以下在 JavaScript 中是什么意思?

var evt=event||window.event;

回答by slebetman

It means that the variable evtis assigned to the value of eventor if eventis undefined it is assigned the value of window.event.

这意味着变量evt被分配给的值event或者如果event未定义它被分配的值window.event

How this works is that in javascript, boolean operators don't evaluate to true or false but instead evaluates to the value of the last object that is not falsy* or the falsy value.

这是如何工作的,在 javascript 中,布尔运算符不会计算为真或假,而是计算为最后一个不是 falsy* 或 falsy 值的对象的值。

So the statement first evaluates the expression event || window.event. If eventis true then the expression does not need to be evaluated any further since an OR only needs one member to be true. Therefore the value of eventis returned. If eventis falsy then the the right side of the OR operator needs to be evaluated to determine if the result is false. In which case, if window.eventis not falsy then its value is returned.

所以语句首先计算表达式event || window.event。如果event为真,则不需要进一步评估表达式,因为 OR 只需要一个成员为真。因此event返回的值。如果event为假,则需要评估 OR 运算符的右侧以确定结果是否为假。在这种情况下,如果window.event不为假,则返回其值。

This is a very common idiom to get the event object from event handlers. On standards compliant browsers, the event object is passed as the first parameter to the event handler. But on IE the event object is a global variable. And for historical reasons, all global variables are members of the window object.

这是从事件处理程序中获取事件对象的一种非常常见的习惯用法。在符合标准的浏览器上,事件对象作为第一个参数传递给事件处理程序。但在 IE 上,事件对象是一个全局变量。而且由于历史原因,所有的全局变量都是 window 对象的成员。

So the code should look something like this:

所以代码应该是这样的:

element.onclick = function (event) {
  var evt = event ||     // use the value of event if available or
            window.event;// if not assume it's IE and use window.event

  /* ... */
}

Note: * falsy values in javascript are: false, 0, null and undefined.

注意:* javascript 中的假值是:false、0、null 和 undefined。

回答by paxdiablo

The code is a hack because Microsoft decided to put their events in the global window.eventinstead of passing it as a parameter to the event function.

该代码是一个黑客,因为微软决定将他们的事件放在全局中,window.event而不是将其作为参数传递给事件函数。

So, this code will attempt to set evtto the event passed in (which will work for the non-Microsoft browsers) and, if that turns out to be null(as it will be for Microsoft browsers), it will then grab it from the global.

因此,此代码将尝试设置evt传入的事件(这将适用于非 Microsoft 浏览器),如果结果如此null(对于 Microsoft 浏览器也是如此),它将从全局获取它.

From that point on, your function can just use evtdisregarding browser differences (well, at least those relating to events).

从那时起,您的函数可以只使用evt浏览器差异(好吧,至少与事件相关的差异)。

回答by Marian07

var evt=event||window.event;

The code above is a shortcut to a IF ELSE statement, and is equivalent to the bellow code:

上面的代码是 IF ELSE 语句的快捷方式,相当于下面的代码:

var evt = "nothing valuable yet"; 
if ( event ) {
   evt = event;
} else {
  evt = window.event;
}



Two IF ELSE shortcuts in Javascript:

Javascript 中的两个 IF ELSE 快捷方式:

var resultIsTrue = true || false; // if first value is true, return first value
var resultIsFalse = true && false; // if first value is true, return second value