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
What is a flag variable?
提问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, 0
or 1
, True
or 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 = 0
and change it to flag = 1
in the program to perform an action.
标志变量通常被赋予一个值,0
或1
,True
或False
。它用作布尔变量,其中结果在 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 true
or false
or is either 0
or 1
(depending on the language and programmer's personal preference).
无论您使用哪种语言,标志变量的概念始终相同:存储布尔值的变量。布尔值是一个始终为true
或false
或为0
或1
(取决于语言和程序员的个人偏好)的值。
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.