admin管理员组文章数量:1356582
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.
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.
Share Improve this question asked Nov 12, 2011 at 18:38 ZeynelZeynel 13.5k31 gold badges103 silver badges148 bronze badges4 Answers
Reset to default 2Just initialize it at declaration:
var count = 0;
And don't declare a second count
within your else
. That's a pletely separate variable.
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.
If you remove the var
withing your else
, things will work as you expect:
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.
It appears that nobody has shown the simple testCount()
implementation. Just initialize it upon declaration and then just use it in your function:
var count = 0;
function testCount ()
{
alert("count is: " + count);
count++;
}
testCount();
Assuming you cant just
var count = 0;
You can detect its lack of an assigned value with;
if (typeof count === 'undefined')
count = 0;
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 :
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 :
function testCount ()
{
...
alert("count is: " + count);
count = 0;
...
};
Note that I have removed the var keyword.
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
本文标签:
版权声明:本文标题:javascript - How do I set up "var count" so that it is initialized the first time and then incremented? - Stac 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744015817a2576307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论