简体   繁体   English

谁能解释一下 e = e || X? 为什么将 e 分配给 e?

[英]Can someone please explain e = e || x? Why assign e to e?

Can anyone explain what this statement means?谁能解释一下这句话的意思?

e = e || x

Specifically,具体来说,

e = e || window.event

This appears in a chunk of code I am looking at.这出现在我正在查看的一大段代码中。

I'm not at a complete loss, however My understanding is that it assigns both e and window.event (or x/whatever) to e.我并没有完全不知所措,但是我的理解是它将ewindow.event (或x/whatever)都分配给e。 It's only natural, right?这很自然,对吧?

But what is the value in assigning e to e ?但是将e分配给e的价值是什么? Shouldn't e = window.event be enough? e = window.event不应该足够吗? Perhaps is depends on how it is used?也许取决于它的使用方式?

e = e || x e = e || x assigns x to e if e evalutes to false.如果e评估为 false,则e = e || xx分配给e

This is the same as:这与以下内容相同:

if (!e) {
  e = x;
}
// or
e = e ? e : x

Here is a table which shows which values evalute to false : https://stackoverflow.com/a/7615236/603003这是一个表格,显示了哪些值评估为falsehttps ://stackoverflow.com/a/7615236/603003

The most important values are: null and undefined .最重要的值是: nullundefined


What does it mean in your context? 在你的上下文中是什么意思? You probably have some sort of this code: 你可能有一些这样的代码:

 function handler(e) { e = e || window.event; }

Where handler is an event listener attached to a DOM element.其中handler是附加到 DOM 元素的事件侦听器。 Since older versions of IE did not pass the event object as a parameter, one had to check if the parameter was undefined .由于旧版本的 IE 没有将事件对象作为参数传递,因此必须检查参数是否为undefined If the latter was the case, one assigns the global window.event object (which IE supplied) to e .如果是后者,则将全局window.event对象(IE 提供)分配给e

It doesn't assign both to "e", just the one that's not either undefined , null , 0 , NaN , "" , or false .它不会将两者都分配给“e”,而只会分配给undefinednull0NaN""false的那个。 It prefers the original value of "e" to window.event because "e" is on the left side of ||它更喜欢 "e" 的原始值而不是window.event ,因为 "e" 在||的左侧, but if it's empty (one of those values I listed) then "e" will be assigned window.event . ,但如果它是空的(我列出的那些值之一),那么“e”将被分配window.event

It's done because Internet Explorer didn't pass the event reference as a parameter, instead simply binding to a global symbol.这样做是因为 Internet Explorer 没有将事件引用作为参数传递,而是简单地绑定到全局符号。 Event handlers were very often written:事件处理程序经常被编写:

function someHandler(e) {
  e = e || window.event;
  // ...
}

It would probably have been more rigorously "correct" to write:写起来可能更严格“正确”:

function pedanticHandler(e) {
  if (e === undefined) // or arguments.length == 0 perhaps
    e = window.event;
  // ...
}

You're misunderstanding operators.你误解了运营商。

This line assigns the expression e || x此行分配表达式e || x e || x to the variable e . e || x到变量e

The value of e || x e || x e || x is the first truthy value. e || x是第一个真值。
If e is truthy, that will be e ;如果e是真的,那将是e if e is falsy, it will be x .如果e是假的,它将是x

it is redundant to assign e = e , they do it as part of this statement because it is an idiom.分配e = e是多余的,他们将其作为此语句的一部分,因为它是一个成语。

The statement checks if e is defined and if it is not then it initializes it with the expression that follows ||该语句检查是否定义了e ,如果没有,则使用||的表达式对其进行初始化。 . . This works because when ||这是有效的,因为当|| expression is evaluated the interpreter stops evaluation when the first true part (from the left) is found.表达式被评估当找到第一个true部分(从左侧)时,解释器停止评估。

In particular, if e evaluates to true then evaluation stops right then and effectively you have e = e , which is redundant.特别是,如果e评估为true ,那么评估就会立即停止,并且实际上您有e = e ,这是多余的。 But if e is undefined or evaluates to false then the right part of the ||但如果e未定义或计算结果为false ,则||的右侧部分is evaluated and assigned to e .被评估并分配给e

I personally would use an if statement instead of being clever.我个人会使用if语句而不是聪明。 Or restructure the code even more to avoid if altogether.或者if更多地重构代码以完全避免。

EDIT: I think the original code is buggy.编辑:我认为原始代码有问题。 Clearly the intention is check if e is already initialized.显然,目的是检查e是否已经初始化。 But here it can be reassigned to itself if it is already initialized and evaluates to true .但是,如果它已经初始化并评估为true ,则可以将其重新分配给自身。 This can have unwanted side effects.这可能会产生不必要的副作用。

If e is undefined (or null , or any other false value), it is initialized with x .如果e undefined (或null或任何其他false值),则使用x对其进行初始化。

It is implicitly :它是隐含的:

var e = e ? e : x;

It doesn't assign both values to e .它不会将两个值都分配给e It's just a way of assigning x to e if the original value of e is null , undefined , 0 , false , NaN , or an empty string ( "" ).如果e的原始值为nullundefined0falseNaN或空字符串 ( "" ),这只是将x分配给e的一种方式。 If the original value of e doesn't match any of the aforementioned conditions, it keeps the original value.如果e的原始值不符合上述任何条件,则保留原始值。

Basically, it's a shorthand form for:基本上,它是以下的简写形式:

if(!e) {
   e = x;
}

The above answer (ComFreek) is correct.上述答案(ComFreek)是正确的。 The reason it does this is because of lazy evaluation.它这样做的原因是因为惰性评估。 The boolean x || y布尔值x || y x || y , evaluated lazily will check x first. x || y ,懒惰地评估将首先检查x If it evaluates to TRUE (ie is non-zero, non-null), then the expression stops and returns TRUE.如果它的计算结果为TRUE (即非零、非空),则表达式停止并返回 TRUE。 If x evaluates to FALSE , it will return y .如果x评估为FALSE ,它将返回y

This is clever code.这是聪明的代码。 Clever is stupid.聪明是愚蠢的。 (opinion) As a maintainer, I prefer to see (观点)作为维护者,我更喜欢看

if (!e) {
    e = x;
}

It set e equal to either itself (if it is not null, undefined or false) otherwise window.event.它设置 e 等于它本身(如果它不为 null、未定义或 false),否则为 window.event。

It is like saying这就像说

if (!e) e = window.event;

In your example e = e || window.event;在你的例子中e = e || window.event; e = e || window.event; is equivalent to :相当于:

if(!e){
     e = window.event;
}

when you add an eventhandler to an element当您将事件处理程序添加到元素时

document.addEventListener('click',handler,false);

in most browsers it passes the event as first parameter.在大多数浏览器中,它将事件作为第一个参数传递。

handler=function(e){// e is the event in some browsers
 e=e||window.event; // but in some old browsers the event is window.event
  // so you check if e(event) exists else you use window.event.
  // '||' means or...
  // e is already defined as a placeholder in the handler function
  // so you don't need to put var infront of it
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM