javascript 什么是标志变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17402125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:11:48  来源:igfitidea点击:

What is a flag variable?

javascriptvariablesflags

提问by HIRA THAKUR

Recently I came across flag variables, but I have no idea what they do.

最近我遇到了标志变量,但我不知道它们是做什么的。

I am little unsure about when to use a flag variable and how to go about it.

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

I Googled it but there weren't any specific examples related to my context (of JavaScript).

我在谷歌上搜索了它,但没有任何与我的上下文(JavaScript)相关的具体示例。

回答by Danny Beckett

Flag Variables Defined and Usessays:

定义和使用的标志变量说:

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. Using flag variable(user defined variable, not a predefined) you can control the flow of a function or statement, allowing you to check for certain conditions while your function progresses.

标志变量,最简单的形式,是您定义的一个变量,直到某些条件为真时才具有一个值,在这种情况下,您可以更改变量的值。使用标志变量(用户定义的变量,而不是预定义的),您可以控制函数或语句的流程,允许您在函数进行时检查某些条件。

As an example:

举个例子:

// errors is the flag var
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!");

回答by sajin

A flag is a variable used to have some value until some condition becomes true,then we change it to false and print the output (Initially flag considered as true)

标志是一个变量,用于在某些条件变为真之前具有某些值,然后我们将其更改为假并打印输出(最初将标志视为真)

回答by sajin

Flag variables are the same for all languages, whether it's RUBY, Python, Javascript or C++.

所有语言的标志变量都是相同的,无论是 RUBY、Python、Javascript 还是 C++。

A flag variable is usually given one value, 0or 1, Trueor False. It's used as a Boolean variable where the result toggles between 0 (False) and 1 (True) or as used by the programmer. Some prefer flag = 0and change it to flag = 1in the program to perform an action.

标志变量通常被赋予一个值,01TrueFalse。它用作布尔变量,其中结果在 0 (False) 和 1 (True) 之间切换或由程序员使用。有些人更喜欢flag = 0并将其更改为flag = 1在程序中执行操作。

回答by Aryan Beezadhur

No matter what language you are using, the concept of flag variables is always the same: a variable that stores a booleanvalue. A boolean value is one which is always either trueor falseor is either 0or 1(depending on the language and programmer's personal preference).

无论您使用哪种语言,标志变量的概念始终相同:存储布尔值的变量。布尔值是一个始终为truefalse或为01(取决于语言和程序员的个人偏好)的值。

Flag variables (also called boolean variables, or often just flags) are used to indicate a condition which can only be either of the two boolean values. The value of the variable can toggle depending on events in the program.

标志变量(也称为boolean variables,或通常只是flags)用于指示只能是两个布尔值之一的条件。变量的值可以根据程序中的事件切换。

A JavaScript example:

一个 JavaScript 示例:

var isRaining = false; // It is either raining or not raining.

var statusCode = true; // Status codes may be boolean values.