【发布时间】:2017-05-26 04:10:52
【问题描述】:
所以在我的 React 项目中,我有一个地址表单的后备默认状态,如下所示:
state = {
newLocation: this.props.location || {
lat: null,
lng: null,
street: null,
complement: null,
neighbourhood: null,
city: null,
state: null,
zipCode: null,
country: null,
},
}
然而,将这么大的对象定义为所有键都为空,这感觉相当荒谬。有没有更短的方法来做到这一点? ES6/ES7 中有什么?
【问题讨论】:
-
它们是否绝对需要明确设置为
null?任何使用这些属性的代码都不能只接受undefined作为一个空值,这样你就可以拥有:newLocation: this.props.location || {}。
标签: javascript object ecmascript-6 ecmascript-2016