This gives to abc
the value of def
if it isn't falsy (i.e. not false
, null
, undefined
, 0
or an empty string), or the value of ghi
if not.
This is equivalent to:
var abc;
if (def) abc = def;
else abc = ghi;
This is commonly used for options:
function myfunc (opts) {
var mything = opts.mything || "aaa";
}
If you call myfunc({mything:"bbb"})
it uses the value you give. It uses "aaa"
if you provide nothing.
In this case, in order to let the caller wholly skip the parameter, we could also have started the function with
opts = opts || {};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…