javaScript(variable)
var s = s || {};
s.c = {};
what purposes it will be use?
javaScript(variable)
var s = s || {};
s.c = {};
what purposes it will be use?
var s = s || {};
This means that if s
is null
, undefined
or false
(it computes to false), then an empty object {}
will be assigned to s
, so that the second line would not cause an error.
But this notation is inacurate. It should be something like:
var s = (typeof s == 'object') ? s : {};
because in the first example if s
is a number the second line will still cause an error.
In the second example notation A ? B : C;
is equal to:
if(A){
B;
}else{
C;
}