简体   繁体   English

使用解构将赋值分配给已定义的变量

[英]Using destructuring assign vaules to already defined varibales

As per javascript.info, the below code should work, https://javascript.info/destructuring-assignment#object-destructuring 按照javascript.info,以下代码应该可以使用, https://javascript.info/destructuring-assignment#object-destructuring

In my project, I already have some variables defined, now I am trying to assign a value using destructuring But, I am unable to run below code. 在我的项目中,我已经定义了一些变量,现在我正在尝试使用destructuring分配值,但是,我无法在代码下运行。

 // This also does not work. let title, width, height; myobj = {title: "Menu", width: 200, height: 100} ({title, width, height}) = myobj; console.log(title); 

 // This also does not work. let title, width, height; myobj = {title: "Menu", width: 200, height: 100} {title, width, height} = myobj; console.log(title); 

You need to wrap the entire expression in () and add a semicolon on the previous line 您需要将整个表达式包装在()并在前一行添加分号

From Assignment without declaration 分配而不声明

The parentheses ( ... ) around the assignment statement are required when using object literal destructuring assignment without a declaration. 使用不带声明的对象文字解构分配时,需要在赋值语句周围加上括号(...)。

Your ( ... ) expression needs to be preceded by a semicolon or it may be used to execute a function on the previous line. 您的(...)表达式必须以分号开头,或者可以将其用于执行上一行的功能。

 // This also does not work. let title, width, height; let myobj = { title: "Menu", width: 200, height: 100 }; // <- semicolon here ({ title, width, height } = myobj); console.log(title); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM