gpt4 book ai didi

javascript - 左手赋值无效,检查变量并在 IF 语句中设置另一个变量

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

if(a=="check" && b=some())

if 语句将(理论上)在 a 等于“check”时返回 true 并且如果 some() 返回某些东西不是 false/0/null/undefined。

但这会引发错误: ReferenceError: Invalid left-hand side in assignment.

在上下文中:

function some(){
return (window.title=="Special")? 0 : window.title;
}
function check(a){
var b;
if(a == 1 && b = some()){
alert("Window's title is: "+b);
}
}

我如何使用两个 if 语句来做到这一点?可能吗?

最佳答案

() 将其分组。

if( a=="check" && ( b=some() ) )

http://jsfiddle.net/KsWBq/

这将计算赋值表达式,然后该表达式的结果将被 if() 语句使用。

关于javascript - 左手赋值无效,检查变量并在 IF 语句中设置另一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4981878/

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