本文介绍了如何将对象分解为已定义的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下内容会产生语法错误:
The following produces a syntax error:
let source,
screenings,
size;
source = {
screenings: 'a',
size: 'b'
};
{
screenings,
size
} = source;
预期结果:
screenings should be equal to 'a'
size should be equal to 'b'
推荐答案
您需要使用没有声明的分配语法:
({
screenings,
size
} = source);
从链接的文档中:
显然,您需要使用它,因为您无法重新声明let
变量.如果您使用的是var
,则可以重新声明var { screenings, size } = source;
And obviously you need to use this as you can't redeclare a let
variable. If you were using var
, you could just redeclare var { screenings, size } = source;
这篇关于如何将对象分解为已定义的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!