如何在 ES6 中为空的解构对象赋值? [已关闭]

问题描述 投票:0回答:1

我有以下内容:

const { var1 } = requestObj1 //requestObj1.var1 = {} => and hence return {}
var1.anotherVariable = reqObj2?.anotherObj?.value //return error: cannot read property of undefined.
requestObj1: { headers: {}, method: 'GET', timeout: 30000, uri: 'some url'

}

我认为我们不能在未定义的对象上使用链接。

javascript
1个回答
0
投票

您可以指定一个默认值,以便

var1
永远不会未定义:

const { var1={} } = requestObj1
var1.anotherVariable = reqObj2?.anotherObj?.value 
© www.soinside.com 2019 - 2024. All rights reserved.