javascript 如何设置“var count”以便它在第一次初始化然后递增?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8106644/
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
How do I set up "var count" so that it is initialized the first time and then incremented?
提问by Zeynel
I tried this:
我试过这个:
var count;
function testCount ()
{
if (count)
{
alert("count is: " + count);
count++;
}
else
{
alert("count is: " + count);
var count = 0;
}
};
testCount();
but it always gives "undefined" and the value of count is not updated when I run it again in jsfiddle.
但它总是给出“未定义”,并且当我在 jsfiddle 中再次运行它时,计数的值不会更新。
回答by Oded
Just initialize it at declaration:
只需在声明时初始化它:
var count = 0;
And don't declare a second count
within your else
. That's a completely separate variable.
并且不要count
在您的else
. 这是一个完全独立的变量。
The problem in your code is that the count
within the else
block is different from the one you have declared in the global scope. This happens because you are defining a new variable with the same name by using var count = 0;
within the else
clause which will not be referring to the count
variable declared outside of the function.
您代码中的问题是块count
内的else
与您在全局范围内声明的不同。发生这种情况是因为您通过var count = 0;
在else
子句中使用来定义一个具有相同名称的新变量,该子句不会引用count
在函数外部声明的变量。
If you remove the var
withing your else
, things will work as you expect:
如果您删除var
withing else
,事情将按您预期的那样工作:
var count;
function testCount ()
{
if (count)
{
alert("count is: " + count);
count++;
}
else
{
alert("count is: " + count);
count = 0;
}
};
testCount();
However, if you call testCount
again, the if
will evaluate to false
, as 0
is a "falsey" value in Javascript.
但是,如果您testCount
再次调用,if
将评估为false
,就像0
Javascript 中的“falsey”值一样。
回答by Alex K.
Assuming you cant just
假设你不能只是
var count = 0;
You can detect its lack of an assigned value with;
您可以使用以下方法检测它是否缺少指定值;
if (typeof count === 'undefined')
count = 0;
回答by jfriend00
It appears that nobody has shown the simple testCount()
implementation. Just initialize it upon declaration and then just use it in your function:
似乎没有人展示过简单的testCount()
实现。只需在声明时初始化它,然后在您的函数中使用它:
var count = 0;
function testCount ()
{
alert("count is: " + count);
count++;
}
testCount();
回答by fflorent
It is a problem of variable scope.
这是变量作用域的问题。
When outside a function, you define a variable, this variable is global. In other word, when you do :
当在函数之外时,你定义了一个变量,这个变量是全局的。换句话说,当你这样做时:
var count;
You declare count, and you can use it everywhere.
你声明计数,你可以在任何地方使用它。
Now, in a function, when you declare count as below :
现在,在函数中,当您声明 count 时:
function testCount ()
{
...
alert("count is: " + count);
var count = 0;
...
};
you declare a new variable whose scope is in the function, and initialize it with 0. However, what you want is to change the global variable value. So, just do that :
你声明了一个作用域在函数中的新变量,并用0初始化它。然而,你想要的是改变全局变量的值。所以,就这样做:
function testCount ()
{
...
alert("count is: " + count);
count = 0;
...
};
Note that I have removed the varkeyword.
请注意,我已删除var关键字。
Now, to simplify the whole program, I suggest you that :
现在,为了简化整个程序,我建议您:
var count = 0;
function testCount ()
{
alert("count is: " + count);
count++;
};
- the initialization is made with the declaration
- so you do not need to worry about the initialization inside testCount
- your code is lighter
- 初始化是用声明进行的
- 所以你不需要担心 testCount 里面的初始化
- 你的代码更轻