What it means is that if color[idx]
is "falsy," use an empty array instead. "Falsy" values are false
(of course), 0
, NaN
, ""
, undefined
, and null
(all other values are "truthy"). That idiom is an example of JavaScript's curiously powerful ||
operator*.
In this case, it's supplying a default value if color
doesn't contain a property with the name contained by idx
(because when you index into an object like that and the key doesn't match any existing property name, the result is undefined
): x
will be 1
(if idx
is "yellor"), 2
(if idx
is "red"), 0
(if idx
is "black"), or []
if idx
is anything else.
So answering your question at the end of your question, basically, yes. It's:
var x = color[idx];
if (!x) {
x = [];
}
or
var x = color[idx] ? color[idx] : [];
* (That's a post on my anemic little blog.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…