我正在尝试从循环内部更新存在于循环外部的变量,如下所示:
var firstRun = true;
console.log("firstRun is " + firstRun);
if(firstRun == true){
console.log("This is your first run");
firstRun = false;
}else{
console.log("You have already run this loop at least once");
};
假设这个代码块在一个更大的代码块内,它将运行 4 次,我希望它输出This is your first run
一次,然后输出You have already run this loop at least once
3 次。相反,我得到This is your first run
了 4 次,并且console.log("firstRun is " + firstRun);
总是输出true
我确定这是我不太了解的范围问题。原谅我,我来自红宝石的土地:)