【发布时间】:2020-06-29 17:55:44
【问题描述】:
我有一个函数,我正在尝试从选项中解构几个参数
fetchArtifactsWithFiltersOG = async options => {
const { nextPage, fromSearch } = options;
}
问题:
但是,在某些用例中,options are not passed 会抛出 nextPage of undefined error。
在这种情况下我如何解构,我不确定选项是否只是有时通过。没有使我的语法 ES5。
【问题讨论】:
-
为什么不给它们分配默认值?像 const { nextPage = null, fromSearch } = options;然后你可以像 if( nextpage ) { do someting.....} else { do something else }
-
^ 如果 options 为空怎么办?当它无限期地抛出错误时,这就是我在帖子中所指的。 @parseshyam
标签: javascript ecmascript-6 destructuring