使用解构为已定义的变量赋值
Using destructuring assign vaules to already defined varibales
根据 javascript.info,下面的代码应该可以工作,
https://javascript.info/destructuring-assignment#object-destructuring
在我的项目中,我已经定义了一些变量,现在我尝试使用 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);
您需要将整个表达式包裹在()
中并在上一行添加一个分号
来自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);
根据 javascript.info,下面的代码应该可以工作, https://javascript.info/destructuring-assignment#object-destructuring
在我的项目中,我已经定义了一些变量,现在我尝试使用 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);
您需要将整个表达式包裹在()
中并在上一行添加一个分号
来自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);