Can someone explain what this does?
var foo = foo || alert(foo);
So you need to use var FOO = FOO || {}; which means "FOO will be assigned to FOO (if it exists already) or a new blank object (if FOO does not exist already).
Foo (pronounced FOO) is a term used by programmers as a placeholder for a value that can change, depending on conditions or on information passed to the program. Foo and other words like it are formally known as metasyntactic variables.
If foo
is already defined and evaluates to true, it sets foo = foo
, i.e. it does nothing.
If foo
is defined but evaluates to false, it would popup whatever foo
is (false
, null
, undefined
, empty string, 0, NaN), but since alert
returns nothing, foo
will be set to undefined
.
If foo
is not yet defined, an exception will be thrown. (Edit: In your example, foo
will always be defined because of the var foo
declaration.)
If foo
evaluates to false (e.g. false, null or zero), the value after the ||
operator is also evaluated, and shows the value.
The alert
method doesn't return a value, so foo
will become undefined if it evaluated to false, otherwise it will be assigned it's own value.
var foo;
if (foo)
foo = foo;
else
foo = alert(foo); // probably undefined
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