javascript - 什么是标志变量?

标签 javascript variables flags

最近我遇到了标志变量,但我不知道它们的作用。

我不太确定何时使用标志变量以及如何使用它。

我用 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/

相关文章:

php - 如何在 php 代码中的 css 类上应用 if-else?

javascript - PHP时间戳转换

javascript - Emacs - 用另一个字符串模式替换一些字符串模式

c - C 中短变量的值

css - 如何在 SCSS/COMPASS 中编写浏览器标志?

c# - [Flags] 属性的真正作用是什么?

python - 如何嵌入在 C++ 中设置标志的 python 代码

javascript - 加载内容后Ajax点击功能不起作用

JavaScript 游戏启动速度很快,但随着时间的推移速度会变慢

linux - 存储文件名的变量