最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I set up "var count" so that it is initialized the first time and then incremented? - Stac

programmeradmin0浏览0评论

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 badges
Add a ment  | 

4 Answers 4

Reset to default 2

Just 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论