This question already has answers here:
object destructuring without var

(3个答案)


5年前关闭。




以下产生语法错误:

let source,
    screenings,
    size;

source = {
    screenings: 'a',
    size: 'b'
};

{
    screenings,
    size
} = source;

预期结果:
screenings should be equal to 'a'
size should be equal to 'b'

最佳答案

您需要使用assignment without declaration语法:

({
    screenings,
    size
} = source);

Babel REPL Example

从链接的文档中:



显然,您需要使用它,因为您无法重新声明let变量。如果您使用的是var,则只需重新声明var { screenings, size } = source;

10-01 03:57
查看更多