最近我遇到了标志变量,但我不知道它们的作用。
我不太确定何时使用标志变量以及如何使用它。
我用 Google 搜索了它,但没有任何与我的(JavaScript)上下文相关的具体示例。
最佳答案
Flag Variables Defined and Uses说:
A flag variable, in its simplest form, is a variable you define to have one value until some condition is true, in which case you change the variable's value. It is a variable you can use to control the flow of a function or statement, allowing you to check for certain conditions while your function progresses.
举个例子:
// errors is the flag variable
var errors = 0;
for(var i = 0; i < 10; i++) {
if(i == 6) { // Your error condition
errors++;
}
}
if(errors) { // Is the flag "up"? (i.e. > 0)
alert("There was a problem!");
}
关于javascript - 什么是标志变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17402125/