【发布时间】:2021-12-16 23:42:27
【问题描述】:
colorX 是我的某个函数的参数。
colorX = typeof colorX !== ('undefined' || 'null' || '') ? colorX : 'abc';
对于“未定义”,我将 abc 作为值。但是当我将 null 或 'null' 或空字符串作为参数输入传递时。我遇到了错误。
还有 null 和 'null' 的区别是什么?
【问题讨论】:
-
'null'是一个字符串,而null实际上是 null -
typeof … !== ('undefined' || 'null' || '')没有任何意义。对于所有用途,它都等同于typeof … !== 'undefined'。 -
正如@Bergi 所说,OR 运算符不能正常工作,您必须明确说明所有比较:
x == 'undefined' || x == ''。您可以使用.includes:let ok = ['undefined', 'string']; console.log(ok.includes(typeof colorX))缩短它。
标签: javascript function parameters null undefined