gpt4 book ai didi

javascript - javascript中赋值表达式周围的括号是什么意思

转载 作者:行者123 更新时间:2023-12-04 07:38:30 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Object destructuring without var, let or const

(3 个回答)


3个月前关闭。




我阅读了以下代码 here:

({ a, b } = { a: 10, b: 20 });
console.log(a); // 10
console.log(b); // 20
那和以下有什么区别:
{ a, b } = { a: 10, b: 20 };
console.log(a); // 10
console.log(b); // 20
考虑到它们的结果相同。
附言重点是赋值表达式周围的括号

最佳答案

你的第二个例子不应该工作。您应该会看到一个错误。

let a, b;
{ a, b } = { a: 10, b: 20 };

// Uncaught SyntaxError: Unexpected token '='
原因是大括号 { a, b }被视为块而不是对象文字。
// This is a block
{
console.log('Hello world');
}
当被括号包围时,它被认为是一个对象字面量
({ hello: world })

Note: The parentheses ( ... ) around the assignment statement arerequired when using object literal destructuring assignment without adeclaration.

{a, b} = {a: 1, b: 2} is not valid stand-alone syntax, as the {a, b}on the left-hand side is considered a block and not an object literal.

However, ({a, b} = {a: 1, b: 2}) is valid, as is const {a, b} = {a: 1,b: 2}

Your ( ... ) expression needs to be preceded by a semicolon or it maybe used to execute a function on the previous line.


来源: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#assignment_without_declaration

关于javascript - javascript中赋值表达式周围的括号是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67619108/

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