gpt4 book ai didi

javascript - 使用 if else 简写进行多变量更新

转载 作者:行者123 更新时间:2023-11-28 17:35:10 25 4
gpt4 key购买 nike

我想使用 if else 简写更新多个值,如下所示:

let a = 0;
let b = 1;
let bool = true;

if(bool) {
a = 4;
b = 7;
}

像这样的简写:

bool ? a = 8, b = 10 : null

但是意外的 token ,

最佳答案

您可以使用逻辑 AND。

bool && (a = 8, b = 10);

Documentation说:

You can use the comma operator when you want to include multiple expressions in a location that requires a single expression.

关于javascript - 使用 if else 简写进行多变量更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49296009/

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