What does "var a = a || {};" mean?
I'm a newer for javascript.
Anyone tell me about this?
Thanks!
|
|
0
|
|
|
|
Reply
|
Lison
|
3/26/2011 7:56:33 AM |
|
There are a couple of principles you need to understand here:
First, the JavaScript logical or (||) operator works by evaluating expressions from left to right and returning the first 'true' result.
Second, a null or undefined variable is considered 'false' in a logical operation, whereas a variable with a value is 'true'.
Therefore, this statement is saying:
"If existing variable 'a' (a function argument?) is already defined, reference it with this new variable 'a'. If it is not defined, assign a reference to a new object '{}'."
|
|
0
|
|
|
|
Reply
|
Stephen
|
3/26/2011 9:14:49 AM
|
|