如果co.chkShowRSB
为false,则预期结果是什么?我希望它是错误的,但这是它的工作方式吗?又为什么呢?var test = chkShow:co.chkShowRSB || true;
最佳答案
好的,现在您已经得到图片了,x = false || true;
分配为true。为什么?知道使用short-circuit操作符与执行操作一样很容易:
x = (false ? false : true);
但是,它主要用于设置函数参数的默认值,因此我猜您假设如果第一个是
x
,则将undefined
分配给第二个操作数。除了显式检查未定义的值外,没有办法专门排除未定义的值。然后,您应该使用:x = val === undefined ? defaultVal : val;
或者,因为
undefined
不必是undefined
,并且您要绝对确定:x = (function(val,undefined)//second argument will be the true undefined value
{
return (val === undefined ? defaultVal : val);
})(val);//don't pass second argument