gpt4 book ai didi

javascript - 使用解构为已定义的变量赋值

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:56:30 24 4
gpt4 key购买 nike

根据 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 - 使用解构为已定义的变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57008099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com