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?
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".
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).
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.
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.
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.
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
.
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;
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With