【发布时间】:2018-12-21 08:39:42
【问题描述】:
抱歉,我知道这确实是一个愚蠢的问题,但经过几轮谷歌搜索后我找不到答案。代码如下:
let x = 0
// After some calculation I know the obj should be:
const obj = {'x': 1 }
// Then how to destructuring assigment at this line
{ x } = obj // this is incorrect
// But if I use: x = obj.x, ESLint warns me: [eslint] Use object destructuring. (prefer-destructuring)
console.log(x);
所以我的问题是如何在定义x 之后使用解构赋值。
【问题讨论】:
-
我没有发现问题,
const obj = {'x': 1}; const { x } = obj;工作正常。你能显示更多代码吗? -
@SamHolmes 谢谢!我从你的链接中得到了答案!
-
@dhilt 我在解构赋值之前定义了
let x = 0。这与您的示例代码不同,您的是在定义时对 x 进行解构分配。谢谢大家!