如何将对象分解为已定义的变量? [英] How to destructure an object to an already defined variable?
本文介绍了如何将对象分解为已定义的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下内容会产生语法错误:
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'
推荐答案
您需要使用 从链接的文档中:
在以下情况下,赋值语句周围的(..)是必需的语法 使用没有声明的对象字面量解构赋值
The ( .. ) around the assignment statement is required syntax when using object literal destructuring assignment without a declaration
显然,您需要使用它,因为您无法重新声明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;
这篇关于如何将对象分解为已定义的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文