空值合并运算符 '??'的操作和运用

空值合并操作符(??)是一个逻辑操作符,当左侧的操作数为 null 或者 undefined 时,返回其右侧操作数,否则返回左侧操作数。

与逻辑或操作符(||)不同,逻辑或操作符会在左侧操作数为假值时返回右侧操作数。也就是说,如果使用 || 来为某些变量设置默认值,可能会遇到意料之外的行为。比如为假值(例如,'' 或 0)时。见下面的例子。

 
 
 
  1. const foo = null ?? 'default string'; 
  2. console.log(foo); 
  3. // expected output: "default string" 
  4.  
  5.  
  6. const baz = 0 ?? 42; 
  7. console.log(baz); 
  8. // expected output: 0 

语法

 
 
 
  1. leftExpr??rightExpr 

示例
使用空值合并操作符

在这个例子中,我们使用空值合并操作符为常量提供默认值,保证常量不为 null 或者 undefined。

 
 
 
  1. const nullValue = null; 
  2. const emptyText = ""; // 空字符串,是一个假值,Boolean("") === false 
  3. const someNumber = 42; 
  4.  
  5. const valA = nullValue ?? "valA 的默认值"; 
  6. const valB = emptyText ?? "valB 的默认值"; 
  7. const valC = someNumber ?? 0; 
  8.  
  9. console.log(valA); // "valA 的默认值" 
  10. console.log(valB); // ""(空字符串虽然是假值,但不是 null 或者 undefined) 
  11. console.log(valC); // 42 

为变量赋默认值

以前,如果想为一个变量赋默认值,通常的做法是使用逻辑或操作符(||):

 
 
 
  1. let foo; 
  2.  
  3. //  foo is never assigned any value so it is still undefined 
  4. let someDummyText = foo || 'Hello!'; 

然而,由于 || 是一个布尔逻辑运算符,左侧的操作数会被强制转换成布尔值用于求值。任何假值(0, '', NaN, null, undefined)都不会被返回。这导致如果你使用0,''或NaN作为有效值,就会出现不可预料的后果。

 
 
 
  1. let count = 0; 
  2. let text = ""; 
  3.  
  4. let qty = count || 42; 
  5. let message = text || "hi!"; 
  6. console.log(qty);     // 42,而不是 0 
  7. console.log(message); // "hi!",而不是 "" 

空值合并操作符可以避免这种陷阱,其只在第一个操作数为null 或 undefined 时(而不是其它假值)返回第二个操作数:

 
 
 
  1. let myText = ''; // An empty string (which is also a falsy value) 
  2.  
  3. let notFalsyText = myText || 'Hello world'; 
  4. console.log(notFalsyText); // Hello world 
  5.  
  6. let preservingFalsy = myText ?? 'Hi neighborhood'; 
  7. console.log(preservingFalsy); // '' (as myText is neither undefined nor null) 

短路

与 OR 和 AND 逻辑操作符相似,当左表达式不为 null 或 undefined 时,不会对右表达式进行求值。

 
 
 
  1. function A() { console.log('函数 A 被调用了'); return undefined; } 
  2. function B() { console.log('函数 B 被调用了'); return false; } 
  3. function C() { console.log('函数 C 被调用了'); return "foo"; } 
  4.  
  5. console.log( A() ?? C() ); 
  6. // 依次打印 "函数 A 被调用了"、"函数 C 被调用了"、"foo" 
  7. // A() 返回了 undefined,所以操作符两边的表达式都被执行了 
  8.  
  9. console.log( B() ?? C() ); 
  10. // 依次打印 "函数 B 被调用了"、"false" 
  11. // B() 返回了 false(既不是 null 也不是 undefined) 
  12. // 所以右侧表达式没有被执行 

不能与 AND 或 OR 操作符共用

将 ?? 直接与 AND(&&)和 OR(||)操作符组合使用是不可取的。(译者注:应当是因为空值合并操作符和其他逻辑操作符之间的运算优先级/运算顺序是未定义的)这种情况下会抛出 SyntaxError 。

 
 
 
  1. null || undefined ?? "foo"; // 抛出 SyntaxError 
  2. true || undefined ?? "foo"; // 抛出 SyntaxError 

但是,如果使用括号来显式表明运算优先级,是没有问题的:

 
 
 
  1. (null || undefined ) ?? "foo"; // 返回 "foo" 

与可选链式操作符(?.)的关系

空值合并操作符针对 undefined 与 null 这两个值,可选链式操作符(?.) 也是如此。在这访问属性可能为 undefined 与 null 的对象时,可选链式操作符非常有用。

 
 
 
  1. let foo = { someFooProp: "hi" }; 
  2.  
  3. console.log(foo.someFooProp?.toUpperCase()); // "HI" 
  4. console.log(foo.someBarProp?.toUpperCase()); // undefined 

网站栏目:空值合并运算符 '??'的操作和运用
当前网址:http://www.hantingmc.com/qtweb/news30/368730.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联