简体   繁体   English

当函数的参数为​​“未定义”或“空”或“”(空字符串)时,如何设置它的默认值?

[英]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'有什么区别?

Explanation解释

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 the typeof 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所有接受值。

Checking for null检查是否为空

 // 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

Resulting Code结果代码

Since falsy values can be any of these:由于虚假值可以是以下任何一个:

Besides false , possible falsy expressions are: null , NaN , 0 , the empty string ( "" ), and undefined .除了false ,可能的虚假表达式有: nullNaN0 、空字符串 ( "" ) 和undefined

MDN MDN

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.

相关问题 当值设置为 null 时,Function 返回未定义 - Function returning undefined when value set to null Javascript如何将Object中的null / undefined值设置为空字符串 - Javascript how to set null/undefined values in an Object to an empty string 如果另一个字符串为空,如何在PHP中将字符串的默认值设置为? - How to set default value to a string in PHP if another string is empty? 如何在 javascript function 中设置参数的特定默认值 - How to set value for specific default value of a parameter in a javascript function 默认情况下,如何将ng-model值设置为空字符串 - By Default, How to set ng-model value to an empty string 如何在JavaScript中匹配0和空字符串,但不是0和未定义,而不是0和null? - How to match 0 and an empty string, but not 0 and undefined, and not 0 and null, in JavaScript? 设置函数对象参数的键的默认值 - Set the default value for the key of the function object parameter 为 JavaScript 函数设置默认参数值 - Set a default parameter value for a JavaScript function 传递一个 function 作为参数,并设置一个默认值 - Passing a function as a parameter, and set a default value 在javascript函数中为布尔参数设置默认值 - Set a default value for a boolean parameter in a javascript function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM