Can someone explain that e = e || X? Why assign ee?

Can someone explain what this statement means?

e = e || x 

In particular,

 e = e || window.event 

This appears in the piece of code I'm looking at.

I do not have a complete loss, however, I understand that it assigns e and window.event (or x / whatever) to e. This is natural, right?

But what is the meaning of assigning e to e? Should e = window.event be insufficient? Perhaps depends on how it is used?

+6
source share
10 answers

e = e || x e = e || x assigns x to e if e evalutes to false.

This is the same as:

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

Here is a table that shows which evalute values ​​are false: fooobar.com/questions/3141 / ...

The most important values ​​are: null and undefined.


What does this mean in your context? You probably have some kind of code:
 function handler(e) { e = e || window.event; } 

Where handler is an event listener attached to a DOM element. Since older versions of IE did not pass the event object as a parameter, it was necessary to check whether the parameter was undefined. If the latter was the case, one assigns to the global object window.event (which is provided by IE) in e .

+18
source

It does not assign both e values, not undefined , null , 0 , NaN , "" or false . He prefers the original value of "e" on window.event , because "e" is on the left side || but if it is empty (one of the values ​​I have listed), then "e" will be assigned to window.event .

This is because Internet Explorer did not pass the event reference as a parameter, but simply attached to a global symbol. Event handlers are very often written:

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

Perhaps it would be more strictly "correct" to write:

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

You misunderstand the operators.

This line assigns the variable expression e || x e || x expression e .

The value of e || x e || x is the first true value.
If e is true, it will be e ; if e is false, it will be x .

+4
source

over assign e = e , they do it as part of this statement because it is an idiom.

The operator checks if e exists, and if not, it initializes it with the expression that follows || . This works because when the expression || is evaluated, the interpreter stops evaluating when the first part of true (on the left).

In particular, if e evaluates to true , then the evaluation stops immediately and you have e = e , which is redundant. But if e is undefined or evaluates to false , then the right side || evaluated and assigned e .

I personally would use the if if instead of smart. Or, rebuild the code even further to avoid if at all.

EDIT: I think the source code is broken. Obviously, the goal is to check if e already initialized. But here it can be reassigned to itself if it is already initialized and evaluated as true . This can have unwanted side effects.

+2
source

If e is undefined (or null or any other false value), it is initialized with x .

This is implicit:

 var e = e ? e : x; 
+2
source

It does not assign both e values. This is just a way of assigning x to e if the original value of e is null , undefined , 0 , false , NaN or an empty string ( "" ). If the original value of e does not meet any of the above conditions, it retains the original value.

Basically, this is an abbreviated form for:

 if(!e) { e = x; } 
+2
source

The above answer (ComFreek) is correct. The reason is that this is due to a lazy assessment. Logical x || y x || y evaluated lazily will check x first. If it evaluates to TRUE (that is, nonzero, not empty), then the expression stops and returns TRUE. If x evaluates to FALSE , it will return y .

This is smart code. Smart is stupid. (Opinion) As an attendant, I prefer to see

 if (!e) { e = x; } 
+2
source

It sets e equal to itself (if it is not null, undefined or false), otherwise window.event.

This is how to say

 if (!e) e = window.event; 
+1
source

In your example, e = e || window.event; e = e || window.event; is equivalent to:

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

when you add an event handler to an element

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

in most browsers, it passes the event as the 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 } 
+1
source

Source: https://habr.com/ru/post/949478/


All Articles