[英]How to set default value of a parameter of a function when it is 'undefined' or 'null' or ''(empty string)?
colorX is my parameter from some function. colorX 是我的某个函数的参数。
colorX = typeof colorX !== ('undefined' || 'null' || '') ? colorX : 'abc';
Here for 'undefined' I'm getting abc as value.这里对于“未定义”,我将 abc 作为值。 But when I pass null or 'null' or empty string as parameter input.
但是当我将 null 或 'null' 或空字符串作为参数输入传递时。 I'm getting error.
我收到错误。
Also what is the difference between null and 'null'?另外null和'null'有什么区别?
null
vs 'null'
null
与'null'
When using typeof
, it expects a string version of the type, such as 'undefined'
, 'function'
, 'bigint'
, and so on.使用
typeof
,它需要该类型的字符串版本,例如'undefined'
、 'function'
、 'bigint'
等。 However, specifically for the null
type it functions as an object:但是,特别是对于
null
类型,它用作对象:
console.log(typeof null); // expected output: object console.log(typeof 'strExample'); // expected output: string // We can return a boolean by doing so: let variable = 10; console.log(typeof variable === 'string'); // expected output: false
The explanation as to why can be found on MDN :在解释为什么上可以找到MDN :
In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value.
在 JavaScript 的第一个实现中,JavaScript 值被表示为一个类型标记和一个值。 The type tag for objects was
0
.对象的类型标记为
0
。null
was represented as the NULL pointer (0x00
in most platforms).null
表示为 NULL 指针(在大多数平台中为0x00
)。 Consequently,null
had 0 as type tag, hence thetypeof
return value"object"
.因此,
null
有 0 作为类型标记,因此typeof
返回值"object"
。 ( reference )(参考)
A fix was proposed for ECMAScript (via an opt-in), but was rejected .
提出了针对 ECMAScript 的修复(通过选择加入),但被拒绝。 It would have resulted in
typeof null === 'null'
.它会导致
typeof null === 'null'
。
In any other scenario, 'null'
, 'undefined'
, and 'string'
would be identified as strings since they are surrounded in ''
's, where this is what @reyno was referring to.在任何其他情况下,
'null'
、 'undefined'
和'string'
将被标识为字符串,因为它们被包围在''
's 中,这就是 @reyno 所指的地方。
See all accepting values for typeof
here . 在此处查看
typeof
所有接受值。
// null will be identified as an "object" type; checking // null with typeof is redundant as {} can be the same as null. console.log(typeof null); // expected output: object console.log(typeof {}); // expected output: object // so we can just do: let colorX = null; console.log(colorX === null); // expected output: true
Since falsy values can be any of these:由于虚假值可以是以下任何一个:
Besides
false
, possible falsy expressions are:null
,NaN
,0
, the empty string (""
), andundefined
.除了
false
,可能的虚假表达式有:null
、NaN
、0
、空字符串 (""
) 和undefined
。
We can simply do:我们可以简单地做:
colorX = colorX || 'abc';
As @ivar claims.正如@ivar 声称的那样。
Using the ternary operator you could simplyfy this to使用三元运算符,您可以简单地将其简化为
colorX = colorX ? colorX : 'abc';
The Conditional (ternary) operator syntax is条件(三元)运算符语法是
condition ? exprIfTrue : exprIfFalse
and according to the MDN docs并根据MDN文档
...null, NaN, 0, the empty string (""), and undefined.
...null、NaN、0、空字符串 ("") 和未定义。 If condition is any of these, the result of the conditional expression will be the result of executing the expression exprIfFalse.
如果条件是其中任何一个,条件表达式的结果将是执行表达式 exprIfFalse 的结果。
But alternatively and even shorter (in case you are checking for all 'falsy' values) you might as well use the logical OR但是,或者甚至更短(如果您正在检查所有“假”值),您也可以使用逻辑 OR
colorX = colorX || 'abc';
Notice that请注意
null, undefined, 0
with quotes simply become strings containing the characters without "functional meaning"带引号只是变成包含没有“功能意义”的字符的字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.