[英]destructuring falsey and null with default parameters
I'm trying to understand how falsey and null values are destructured with default parameters. 我试图了解如何使用默认参数对false值和null值进行解构。 Here are some examples I've ran:
以下是我跑过的一些例子:
// #1
const person = { email: 'a@example.com' }
const { email = '' } = person
// email is 'a@example.com'
// #2
const person = { email: '' }
const { email = '' } = person
// email is ''
// #3
const person = { email: false }
const { email = '' } = person
// email is boolean false. why?!
// #4
const person = { email: null }
const { email = '' } = person
// email is null. why?!
Is there a shortcut I could write to destructure falsey and null values for #3 and #4 so that my email is an empty string? 有没有我可以编写的快捷方式来构造#3和#4的falsey和null值,以便我的电子邮件是空字符串?
Only undefined
will cause the default initialiser to run. 只有
undefined
才会导致默认初始化程序运行。 If you want to fall back to your default for all falsy values, use the good old ||
如果你想回到所有假值的默认值,请使用旧的
||
operator: 运营商:
const email = person.email || '';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.