gpt4 book ai didi

javascript - 使用 and 速记 if

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

基本上我想这样做:

x ? console.log("true") : x=55 && console.log("changed!!")

如果xfalse,它会将值更改为55console.log“更改! !”

最佳答案

如果我理解正确的话,您基本上想在三元运算符中运行几条指令?

正确的方法是使用括号和逗号:

x ? console.log("true") : (x=55, console.log("changed!!"));

但是,如果您只是测试 bool 变量,请使用 if 语句,它将使您的代码对每个人都更具可读性:

if (x) {
console.log("true");
}
else {
x = 55;
console.log("changed!!");
}

这看起来更自然、更人性化

关于javascript - 使用 and 速记 if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52931957/

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