用默认参数解构false和null
destructuring falsy and null with default parameters
我试图理解如何用默认参数解构false和null值。以下是我运行的一些示例:
// #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?!
是否有一个快捷方式,我可以写解构假和null值#3和#4,使我的电子邮件是一个空字符串?
只有undefined
会导致默认初始化器在解构和函数参数目标中运行。如果您想退回到所有假值的默认值,请使用旧的||
操作符:
const email = person.email || '';
或者以可变变量为目标,然后使用逻辑或赋值:
let { email } = person;
email ||= '';
相关文章:
- 如果 a 为 false,则 if(a) === if(false)
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 数组在递归方法中设置为null
- 如何将localStorage值设置为false
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 无法获取属性'selectedIndex'的未定义引用或null引用
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 什么是'true'和'false'和'null'
- marker.setVisible(false) 和 marker.setMap(null) 不适用于所有标记
- setVisible(false)删除标记,但不删除setMap(null)
- 在MongoDB中输入用户名数组,并返回相同大小的id数组,对于不存在的用户名为null或false
- jQuery - execCommand(“insertUnorderedList”, false, null);- 与
- 在ClojureScript core.match中匹配false(==未定义,null)
- 用默认参数解构false和null
- 如何在数组 (JS) 中过滤掉 NaN、null、0、false
- 从html表单中提取带有值和id的UI元素,忽略null元素和FALSE复选框
- JavaScript检查value是否只有undefined、null或false
- js条件:如果不是false、null或undefined,那么
- 复选框ng将false值设为null
- 为什么null == 0输出false,因为两者都是假值