Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. It's only natural, right?

But what is the value in assigning e to e? Shouldn't e = window.event be enough? Perhaps is depends on how it is used?

like image 621
muiiu Avatar asked Jul 15 '13 15:07

muiiu


People also ask

Why is the derivative of e to the x equal to e to the x?

The differentiation of e to the power x is equal to e to the power x because the derivative of an exponential function with base 'e' is equal to ex. Mathematically, it is denoted as d(ex)/dx = ex. e to the power x is an exponential function with a base equal to 'e', which is known as "Euler's number".

What's so special about Euler's number e?

Euler's number is an important constant that is found in many contexts and is the base for natural logarithms. An irrational number represented by the letter e, Euler's number is 2.71828..., where the digits go on forever in a series that never ends or repeats (similar to pi).

Why do we use e in exponential functions?

e is the base rate of growth shared by all continually growing processes. e lets you take a simple growth rate (where all change happens at the end of the year) and find the impact of compound, continuous growth, where every nanosecond (or faster) you are growing just a little bit.

What is the meaning of e in maths?

The exponential constant is a significant mathematical constant and is denoted by the symbol 'e'. It is approximately equal to 2.718. This value is frequently used to model physical and economic phenomena, mathematically, where it is convenient to write e.


2 Answers

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 which shows which values evalute to false: https://stackoverflow.com/a/7615236/603003

The most important values are: null and undefined.


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. Since older versions of IE did not pass the event object as a parameter, one had to check if the parameter was undefined. If the latter was the case, one assigns the global window.event object (which IE supplied) to e.

like image 94
ComFreek Avatar answered Nov 24 '22 04:11

ComFreek


It doesn't assign both to "e", just the one that's not either undefined, null, 0, NaN, "", or false. It prefers the original value of "e" to window.event because "e" is on the left side of ||, but if it's empty (one of those values I listed) then "e" will be assigned window.event.

It's done because Internet Explorer didn't pass the event reference as a parameter, instead simply binding to a global symbol. 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;
  // ...
}
like image 26
Pointy Avatar answered Nov 24 '22 02:11

Pointy