I met some syntax, which is not clear for me, like this:
anotherVariable = anotherVariable || {};
var variable = anotherVariable.member = anotherVariable.member || {};
What does the code above mean?
I met some syntax, which is not clear for me, like this:
anotherVariable = anotherVariable || {};
var variable = anotherVariable.member = anotherVariable.member || {};
What does the code above mean?
This is default value setter.
anotherVariable = anotherVariable || {};
if anotherVariable
is falsy
then it'll set {}
in anotherVariable
.
''
, null
, 0
,undefined
,NaN
consider falsy
in javascript.
This is same for
var variable = anotherVariable.member = anotherVariable.member || {};
if anotherVariable.member
is falsy
then {}
will be set in anotherVariable.member
and then anotherVariable.member
will be set in variable
.
Here's a combination of few things
true
in all comparisons{}
is a definition of empty objectSo it means this:
if (anotherVariable != null)
{
anotherVariable = {};
}
It is pretty simple :
var a = b || {};
set variable "a" to "b". If b is null or undefined, set "a" to {}
var variable = anotherVariable.member = anotherVariable.member || {};
set anotherVariable.member
to {}
if it's null or undefined.
then set variable
to anotherVariable.member